3d plots with a matrix

I am either missing an easy solution or trying the impossible.

I am attempting to make a 3d plot of a data set I have, two vectors and a matrix. I am running into issues where the data isn’t being indexed correctly, it overlaps the two points on top of each other. For instance if I have the data set

x=[1,2];
y=[10,25];
z=[0 300; 550 1500];

I can get the proper results if I manually input:

scatter([x[1]],[y[1]],[z[1,1]])
scatter!([x[1]],[y[2]],[z[1,2]])
scatter!([x[2]],[y[1]],[z[2,1]])
scatter!([x[2]],[y[2]],[z[2,2]])

The real data set is much larger so this wouldn’t work, I also don’t think I would be able to translate it into a contour plot. When I try to use scatter([x],[y],[z]) I get the points plotted as if I had done:

scatter([x[1]],[y[1]],[z[1,1]])
scatter!([x[1]],[y[1]],[z[1,2]])
scatter!([x[2]],[y[2]],[z[2,1]])
scatter!([x[2]],[y[2]],[z[2,2]])

I have attempted using different matrix and vector combinations and the results always end similarly. I realize I can use for loops or section the data, but it seems like I am missing something fundamental that should make this trivial.

  • If you want a scatter plot, you can get the x and y coordinates as
X = [xj for _  in y, xj in x]
Y = [yi for yi in y, _  in x]

scatter( X[:], Y[:], Z[:] )

(See for example Meshgrid function in Julia - #26 by henry2004y)

  • Maybe you are also interested in a surface plot, see surface. Here you can directly use your data x, y, z.

More a side note, if you don’t want to create the vectors X and Y you could use GitHub - JuliaArrays/LazyGrids.jl: A Julia package for representing multi-dimensional grids

Worked great thank you! I’ll try to summarize for future reference.

To create a 3d scatter plot the x and y axis need to be represented as a matrix

X = [x1 x2; x1 x2]
Y = [y1 y1; y2 y2]

(Which can be expanded)

X = [x1 x2 x3; x1 x2 x3]
Y = [y1 y1 y1; y2 y2 y2]

To map the data properly. Instead of entering everything manually, the code:

X = [xj for _  in y, xj in x]
Y = [yi for yi in y, _  in x]

Expands the axis vectors into their appropriate matrix

1 Like