I was trying to plot complex numbers in Julia but I haven't found any good way to do it yet.
using PyPlot
nums = ComplexF64.([1,2,4],[2,2,-1])
polar.(Base.vect.(0.0,angle.(nums)),Base.vect.(0.0,abs.(nums)),marker="o")
One way is to plot the real and imaginary part as x and y
julia> using Plots
julia> d = [0.0000000+0.0000000im, 0.1111111+0.0000000im,
0.1666667+0.0962250im, 0.2222222+0.0000000im,
0.3333333+0.0000000im, 0.3888889+0.0962250im,
0.3333333+0.1924501im, 0.4444444+0.1924501im,
0.5000000+0.2886751im, 0.5555556+0.1924501im,
0.6666667+0.1924501im, 0.6111111+0.0962250im,
0.6666667+0.0000000im, 0.7777778+0.0000000im,
0.8333333+0.0962250im, 0.8888889+0.0000000im,
1.0000000+0.0000000im]
julia> plot(real(d),imag(d))
# or directly with plot(d)
Related
I came across a Rose plot obtained with Plots.jl package in Julia:
https://goropikari.github.io/PlotsGallery.jl/src/rose.html
Two things are not clear to me. The first one is what is Julia doing on the line:
θ = 0:2pi/n:2pi
It seems that the output is (lower limit):(bin size):(upper limit) but I haven't seen this type of arithmetics previously where two ranges are divided. The second thing is that I would like to obtain a histogram polar plot as it was done with R (Making a polar histogram in ggplot2), but I haven't found the documentation for line styles or how to do it in Plots.jl. Thanks.
Note that start:step:end is a common syntax in creating ranges. Let's dissect the line:
# `pi` is a reserved variable name in Julia
julia> pi
π = 3.1415926535897...
# A simple division
julia> 2pi/1
6.283185307179586
# Simple multiplication
julia> 2pi
6.283185307179586
So the 0:2pi/n:2pi creates an object of type StepRange that starts from 0 up to 2pi with steps of size 2pi/n.
In the case of desired plot, you can use the PlotlyJS.jl package. As they provided an example here. (Scroll down until you see "Polar Bar Chart")
I tested the code myself, and it's reproducible expectedly. Unfortunately, I don't know anything about the R language.
julia> using RDatasets, DataFrames, PlotlyJS
julia> df = RDatasets.dataset("datasets", "iris");
julia> sepal = df.SepalWidth;
julia> plot(
barpolar(
r=sepal
)
)
Results in:
In Matlab, we would first use [x, y] = meshgrid to generate the grid, then use mesh(x, y, z) to plot the 3D plot. I want to use the same funtionality in Julia Plots.jl, which API should I use? And how can I achieve that?
Thanks a lot in advance!!!
use surface
using Plots
xs = range(-2, stop=2, length=100)\
ys = range(-pi, stop=pi, length=100)
f(x,y) = x*sin(y)
surface(xs, ys, f)
In modern Julia, v1.17, the approach is to create x and y ranges. Julia has changed over the years, and used to have linspace - it doesn't anymore.
There are three ways to create a range:
x = start:step:end
x = range(start,end,step=step)
x = range(start,end,length=npts)
You will also need Plots. If you precompile it, it takes less time to load.
]
pkg > add Plots
pkg > precompile
pkg > Ctrl-C
You need to select your backend for Plots. Choices are:
pyplot() to select PyPlot (also requires Python's MatPlotLib)
plotly() to select Plotly (displays in web browser)
gr() to select GR, the default
Finally, you need to use surface to draw the surface. The function surface can take either a function or a matrix of z values. The function takes two parameters, x and y. Either the function is supplied directly, or it is applied to the ranges:
z = f.(x',y);
One of the ranges is transposed with ', and output suppressed with ;
Surface also takes optional parameters:
fill = :fillname
legend = true | false
size = (width,height)
clims = (lowlimit,highlimit)
An example:
using Plots
plotly()
x=range(-5,5,length=101)
y=range(-5,5,length=101)
function f(x,y)
r = sqrt(x^2+y^2)
sinc(r)
end
z = f.(x',y);
surface(x,y,z,size=(1600,1000),fill=:greens,legend=false)
Is there a way to draw a scatter plot in Julia (preferably with gr backend), in which every point has an arrow pointing to a specified direction on it?
Specifically, my task is to create a gif image with multiple moving points with a small arrow on every point pointing to the direction of its velocity.
So, you want to plot a vector field, right?
The "arrow plot" you are looking for, is usually called quiver-plot in many programming languages. In Julia, too.
If you use Plots.jl the syntax is quiver(x,y,quiver=(u,v)), where x and y are the coordinate vectors and u and v the arrow magnitude vectors.
If you use GR or PyPlot directly the syntax is possibly a bit different.
Small Example
using Plots
gr()
N = 10
x = rand(1:10,N)
y = rand(1:10,N)
u = rand(N)
v = rand(N)
scatter(x,y)
quiver!(x,y,quiver=(u,v))
Consider this minimal example
julia> using Plots
julia> pyplot()
julia> x = -100:0.01:100
julia> y = x
julia> plot(x, y, xscale = :log10)
This raises the following error:
DomainError:
log10 will only return a complex result if called with a complex argument. Try log10(complex(x)).
Of course, this is because the logarithm of negative numbers is not well defined. However, in python you can overcome this problem using the command plt.xscale('symlog'). Is there an equivalent command for Plots.jl?
Let us consider the following scenario .
for x in range (1,100)
for y in range (2,500)
#plot(f(x),g(y))
end
end
where f(x) and g(y) are some user defined functions.
The output must be the desired points on plane.
Is there any way in julia to do like what I need ?
In general I can do like this
for x in range (1,100)
for y in range (2,500)
push!(l,f(x))
push!(m,g(y))
end
end
and then plotting from the two lists l,m as x,y axes respectively.
But now I want to plot points while executing loop.
This is mostly supported in Plots... see https://github.com/tbreloff/Plots.jl/issues/30 for a little more information and some example usage.
use the display function:
for x in 1:100
p = plot(f(x),g(y))
display(p)
sleep(1)
end
(inspired by Andreas Peter on the Julia slack #helpdesk channel)