Polar Plots in Julia - julia

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:

Related

How to plot complex numbers in Julia?

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)

Is there any ax.view_init(elev, azim) equivalent function in plots.jl?

I am using Plots for visualizing 3d-plots with Julia, and I am trying to change camera angle of my plot. In matplotlib in Python, I know that I can use ax.view_init(elev, azim) to change the camera angle, but on Plot.jl, I could not find solution to change the angle.
Is there any equivalent function with ax.view_init(elev, azim) in Python in Julia ?
Example of Plot
using Plots
plot()
for i in 1 : 5
a = rand(10); b= rand(10); c = rand(10);
plot!(a,b,c, seriestype=:scatter)
end
plot!()
As you can read in the manual you can use the camera keyword argument (aliases are: cam, cameras, view_angle, viewangle). This argument sets the view angle for 3D plots. Its value is required to be a tuple (azimuthal, elevation) and the default setting is (30, 30).

Gadfly: How to set number of sampling points when plotting functions?

When plotting a function (as opposed to numerical data), how can we set the number of sample points (i.e. the number of distinct x coordinates where the function is computed)? Importantly, where can I find this information in the documentation?
Example:
plot(x -> sin(1/x), 0.001, 1)
For a useful plot in the 0–0.25 range we need many more points.
One way you can do it is:
using Gadfly;
X=1e-6:1e-6:1.0
plot(x=X, y=X .|> x -> sin(1/x), Geom.line)
or you may like this version more
using Gadfly;
X=[1/z for z=300.0:-0.05:1.0]
plot(x=X, y=X .|> x -> sin(1/x), Geom.line)
To get a docu, just do
?plot
or when you want to look at the code
methods(plot)
The number of sampling points can indeed be specified:
plot(y=[x->sin(1/x)], xmin=[0.001], xmax=[1], Stat.func(1000), Geom.line)
You can find Stat.func in the Gadfly docs here:
http://gadflyjl.org/stable/lib/statistics/#Gadfly.Stat.func.
Note you can write either Stat.func(num_samples=1000) or Stat.func(1000), since there is only one argument.

Is there a way to plot graph in julia while executing loops?

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)

Why is my plot3d white in SciLab?

t = 0:%pi/50:10*%pi;
plot3d(sin(t),cos(t),t)
When I execute this code the plot is done but the line is not visible, only the box. Any ideas which property I have to change?
Thanks
The third argument should, in this case, be a matrix of the size (length arg1) x (length arg2).
You'd expect plot3d to behave like an extension of plot and plot2d but it isn't quite the case.
The 2d plot takes a vector of x and a vector of y and plots points at (x1,y1), (x2,y2) etc., joined with lines or not as per style settings. That fits the conceptual model we usually use for 2d plots - charting the relationship of one thing as a function of another, in most cases (y = f(x)). THere are other ways to use a 2d plot: scatter graphs are common but it's easy enough to produce one using the two-rows-of-data concept.
This doesn't extend smoothly to 3d though as there are many other ways you could use a 3d plot to represent data. If you gave it three vectors of coordinates and asked it to draw a line between them all what might we want to use that for? Is that the most useful way of using a 3d plot?
Most packages give you different visualisation types for the different kinds of data. Mathematica has a lot of 3d visualisation types and Python/Scipy/Mayavi2 has even more. Matlab has a number too but Scilab, while normally mirroring Matlab, in this case prefers to handle it all with the plot3d function.
I think of it like a contour plot: you give it a vector of x and a vector of y and it uses those to create a grid of (x,y) points. The third argument is then a matrix whose dimensions match those of the (x,y) grid holding the z-coordinates of each point. The first example in the docs does what I think you're after:
t=[0:0.3:2*%pi]';
z=sin(t)*cos(t');
plot3d(t,t,z);
The first line creates a column vector of length 21
-->size(t)
ans =
21. 1.
The second line computes a 21 x 21 matrix of products of the permutations of sin(t) with cos(t) - note the transpose in the cos(t') element.
-->size(z)
ans =
21. 21.
Then when it plots them it draws (x1,y1,z11), (x1,y2,x12), (x2,y2,z22) and so on. It draws lines between adjacent points in a mesh, or no lines, or just the surface.

Resources