# 3d plots with a matrix

**URL:** <https://discourse.julialang.org/t/3d-plots-with-a-matrix/87006>\
**Category:** New to Julia\
**Created:** [September 9, 2022, 5:51pm UTC](https://discourse.julialang.org/t/3d-plots-with-a-matrix/87006 "2022-09-09T17:51:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jayo](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Jayo](https://discourse.julialang.org/u/Jayo)\
**Post date:** [September 9, 2022, 5:51pm UTC](https://discourse.julialang.org/t/3d-plots-with-a-matrix/87006/1 "2022-09-09T17:51:08Z")

</div>

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

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

```

I can get the proper results if I manually input:

```julia
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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 9, 2022, 8:36pm UTC](https://discourse.julialang.org/t/3d-plots-with-a-matrix/87006/2 "2022-09-09T20:36:42Z")

</div>

- If you want a scatter plot, you can get the x and y coordinates as

```julia
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](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/26))

- Maybe you are also interested in a surface plot, see [surface](https://makie.juliaplots.org/dev/examples/plotting_functions/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](https://github.com/JuliaArrays/LazyGrids.jl)

---

<div class="post-metadata">

**Author:** ![Jayo](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Jayo](https://discourse.julialang.org/u/Jayo)\
**Post date:** [September 12, 2022, 2:54pm UTC](https://discourse.julialang.org/t/3d-plots-with-a-matrix/87006/3 "2022-09-12T14:54:32Z")

</div>

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

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

```

(Which can be expanded)

```julia
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:

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

```

Expands the axis vectors into their appropriate matrix
