R: How to plot a 3D array - r

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!

Related

How to extract xy-data from Julia plots?

I am using the contour function from Julia's Plots to plot level curves. I want to extract a list of x coordinates and a list of y coordinates corresponding to the level curves from the plot, e.g., something like this. Is there a way to do it in Julia?
Not for contour, unfortunately. For most plot types you can extract the input data of, e.g. the first series in the first subplot, with p[1][1][:x]. But for contour in particular Plots does not generate the level curves, it simply passes the matrix to the backend that then does the computation and display.

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.

R: 3D scatter plot from multiple 3D arrays in scatterplot3d

I'm new to R so please bear with me; I have six 3D arrays (c0, ..., c5) and I need to overlap the individual arrays into one 3D scatter plot. I have one scatter plot using the c0 array with...
library(scatterplot3d) ; scatterplot3d(c0)
... and get the following plot:
Any idea how to add on the other five arrays, with different colors & shapes?
Also, any other suggestion for interactive use? Like python's 3D plots allow for zooming, rotating plot for different views, etc.
UPDATE:
The c0 array is as follows (used read.table('...')); there are 1000 elements, but this will give an idea.
> c0
C4_1548 Mg2_2796 Si4_1393
1 0.703216519 0.611332440 0.715913967
4 -0.073868874 0.333571615 0.174178337
5 0.584873346 -0.398325128 -0.038525721

How do I plot a 3D graph in 2D with color in octave?

I have a function z=f(x,y) and want to plot it using octave, but don't want the plot to be in 3d, as in
octave:1> x=(1:300);
octave:2> y=(1:300);
octave:3> [xx,yy]=meshgrid(x,y);
octave:4> A=sin(xx/100).*yy;
octave:5> mesh(x,y,A)
but rather in 2d using colors for the values of z, like what you get using the gnuplot instruction
gnuplot> plot 'a.txt' matrix w image
if I save the matrix A in the file a.txt. The closest I have found is the command contourf, but the as you can see if you try it,
octave:7> contourf(xx,yy,A)
the result is far from optimal... Any suggestion?
Thanks
imagesc will plot a matrix of your "z" values using colors:
> imagesc(x, y, A)
This will be inverted vertically compared to contourf, but that's easily fixed:
> imagesc(x, flipud(y), flipud(A))
And in your example you don't even need to provide the variables x and y:
> imagesc(A)
> imagesc(flipud(A))

Plotting lines between two points in 3D

I am writing an regression algorithm which tries to "capture" points inside boxes. The algorithm tries to keep the boxes as small as possible, so usually the edges/corners of the boxes go through points, which determines the size of the box.
Problem: I need graphical output of the boxes in R. In 2D it is easy to draw boxes with segments(), which draws a line between two points. So, with 4 segments I can draw a box:
plot(x,y,type="p")
segments(x1,y1,x2,y2)
I then tried both the scatterplot3d and plot3d package for 3D plotting. In 3D the segments() command is not working, as there is no additional z-component. I was surprised that apparently (to me) there is no adequate replacement in 3D for segments()
Is there an easy way to draw boxes / lines between two points when plotting in three dimensions ?
The scatterplot3d function returns information that will allow you to project (x,y,z) points into the relevant plane, as follows:
library(scatterplot3d)
x <- c(1,4,3,6,2,5)
y <- c(2,2,4,3,5,9)
z <- c(1,3,5,9,2,2)
s <- scatterplot3d(x,y,z)
## now draw a line between points 2 and 3
p2 <- s$xyz.convert(x[2],y[2],z[2])
p3 <- s$xyz.convert(x[3],y[3],z[3])
segments(p2$x,p2$y,p3$x,p3$y,lwd=2,col=2)
The rgl package is another way to go, and perhaps even easier (note that segments3d takes points in pairs from a vector)
plot3d(x,y,z)
segments3d(x[2:3],y[2:3],z[2:3],col=2,lwd=2)

Resources