Plots.jl - Map surface color to matrix - julia

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.

Related

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

3D surface with a 2D projection using R

I need to plot a 3D surface with 2D projections like the one below using R.
It features a 3D density plot, something easy to do in R using plotly, for example. The 2D surfaces on the other hand I've had no luck so far in my search for how to draw them. The best I've found is this example, but it uses Python instead of R.
I have also found that package RSM (Response Surface Methods) may have the tools to draw this graph, but I've studied the package documentation and looked for online examples and so far I have not been able to find anything close to this graph in quality.
Base R function persp looks like it could offer some answer too, but I've had no success using it to draw the 2D projections so far. Package plot3D may also offer clues to the solution.
Thanks in advance for any help on this.
Well I do not know how your data looks like but if you don't have an specific data you can use this reference of r-plotly surface.
Here is some example using volcano's data of R.
The trick is use contours()
The code:
# volcano is a numeric matrix that ships with R
plot_ly(z = ~volcano) %>% add_surface(
contours = list(
z = list(
show=TRUE,
usecolormap=TRUE,
highlightcolor="#ff0000",
project=list(z=TRUE)
),
y = list(
show=TRUE,
usecolormap=FALSE, # Projection without colormap
highlightcolor="#ff0000",
project=list(y=TRUE)
),
x = list(
show=TRUE,
usecolormap=TRUE,
highlightcolor="#ff0000",
project=list(x=TRUE)
)
)
)
The output:

newbie: holoviews Curves from pandas dataset columns

I have a pandas dataframe with columns labeled...
x
y
true_x
true_y
I would like to plot a curve of true_x vs true_y overlaid with
points y vs x.
The tutorials leave me baffled since they only describe simple 2D and 3D examples.
We're about to start working extensively on additional documentation so that's good feedback. To create a simple plot like that simply declare a Curve and a Scatter object each with the appropriate kdims and vdims and overlay them using the mul operator:
curve = hv.Curve(df, kdims=['true_x'], vdims=['true_y'])
scatter = hv.Scatter(df, kdims=['x'], vdims=['y'])
curve * scatter

R: How to plot a 3D array

I want to plot a 3D array M where
M <- array(runif(64),dim=c(4,4,4))
A similar question is here with comments that this can be done using a common 3D plot in R, but I could find no such function in R which can be used to plot multidimensional arrays (say, a 3D array as in the above example). Any suggestion how to do it? Thanks.
Use melt to create a table of x,y,z,value, and then rgl to do a 3d plot:
library(reshape2)
library(rgl)
M=melt(M)
points3d(M$Var1,M$Var2,M$Var3)
That's just 64 points in a cube. You can scale and colour them:
points3d(M$Var1,M$Var2,M$Var3,size=10,color=rainbow(10)[M$value*10])
Use whatever method of mapping M$value to colour you prefer. Don't use rainbow palettes for real!

polar image plot, how to do it with R?

I cannot find a straightforward way to make a nice image plot in R, but in polar coordinates. I'm basically attempting to find a R equivalent for the 'polarplot3d' function in MATLAB. I've been playing around with ggplot2 package but without much luck. Am I missing a package that contains functionality for what I'm attempting? thanks in advance for any pointers.
Ok, I'm trying to be more clear about what I'm trying to do. Lets say I want to define a polar coordinate grid, increments in the radial direction are 50m and 2.5 degrees in theta. This should look like a dartboard.
My data (r and angle in below code) are correspond to a radial distance measure and an angle. My desired z-value is the counts of a bivariate histogram between r and angle within the increments described above defining the grid.
My data is like the following:
# synthetic data for angle and distance #
angle <- rnorm(500,mean=90,sd=15)
r <- rnorm(500,mean=700,sd=200)
# bivariate histogram #
observations <- table(cut(angle,breaks=c(seq(0,360,by=2.5))),cut(r,breaks=c(seq(0,1400,by=50))))
# the 'z' data are in observations for each bin of bivariate histogram #
# hot to plot a polar coord image? #
It's very slow to render on my system, but
library(reshape2)
library(ggplot2)
mm <- melt(counts)
ggplot(mm,aes(Var1,Var2,fill=value))+geom_tile()+coord_polar()
ggsave("polar1.png")
appears to work.
I think the following could work. Use mapproject() from the maproj library to transform my xy coordinates acording to a polar projection (or another), Then use as.image() (from fields package) function to build a image object from my new coordiantes and my Z values. Eventually use image.plot().
library("mapproj")
xyProj <- mapproject(x, y, projection="conic", parameters=-90)
library("fields")
im <- as.image(z, x=xyProj)
image.plot(im)

Resources