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

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).

Related

Polar Plots in 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:

What is the equivalent of Matlab's mesh function in Julia Plots.jl

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)

Drawing an arrow with specified direction on a point in scatter plot in Julia

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))

How to plot a circle in julia

I am working on an optimization problem in Julia in which I find the 2D Chebyshev center. I can find the optimal solution but I am at a loss on plotting it.
Lets say the center is c = (x1,y1) and I have radius r of the circle. I need to plot the Chebyshev circle at a center of c and radius r inside of the polygon:
Looking at your plot you are using PyPlot.jl. In this case this is what you can do:
using PyPlot
x = [0,500,600,300,0,0]
y = [0,0,300,500,500,0]
plot(x,y)
plt.gcf().gca().add_artist(plt.Circle((264.978,264.978), 214.976, fill=false))
The general rule for PyPlot.jl is that you can access methods of Python's objects using a getindex with a Symbol passed as method name.

Plots.jl - Map surface color to matrix

I'm trying to figure out how to create surface plots with Plots.jl. I can create a spherical surface from a 2-d mesh like below:
using Plots
plotlyjs()
# Read the theta and phi angles from file...
x2d = sind(theta2d).*cosd(phi2d)
y2d = sind(theta2d).*sind(phi2d)
z2d = cosd(theta2d)
surface(x2d,y2d,z2d)
However, I want to have the surface color be controlled by a separate matrix like the plot below (made with Python).
from mayavi import mlab
# Create/read plot data...
mlab.figure(bgcolor=(1,1,1), fgcolor=(0.,0.,0.))
mlab.mesh(x2d, y2d, z2d, scalars=p2d, colormap='jet', vmax=5, vmin=-35)
Perhaps I should just use the Python plotting functions directly? Or maybe GLVisualize directly?
Thanks!
You can supply the matrix controlling the colors as the fill_z keyword. It doesn't work on all backends, but try plotljys and pyplot.

Resources