# How can I combine different density plots in a 3D plot?

**URL:** <https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194>\
**Category:** Visualization\
**Tags:** plotting, statistics, plots, 3d, probability\
**Created:** [March 16, 2023, 4:34pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194 "2023-03-16T16:34:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 16, 2023, 4:34pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/1 "2023-03-16T16:34:39Z")

</div>

Hi all!

I’m analyzing some data that follows a more-or-less-linear dependency. Here’s a picture:  
 ![bics_binary_weight_plot](https://global.discourse-cdn.com/julialang/original/3X/b/2/b2bfc2c8457994d935b46f0c33a8f6ac820a9e80.png)

I’d like to show the dispersion around the mean for each of the vertical bins you see. What I need is (I think) the `density()` function, so to have a nice plot of the dispersion. However, `density()` returns a plot, not data. **I need to combine all these plots toghether, possibly in 3 dimensions. How can I do that?**

For reference, I’m thinking about something like these (pictures found on the web and with no connection):  
 ![2D/3D Acoustic Propagation Modeling](https://global.discourse-cdn.com/julialang/original/3X/2/7/27dc8b7bd90674be7c134894e15a248d9a0fba33.gif)  
^ This, but of course “following” my data  
 ![Screenshot from 2023-03-16 17-32-18](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae9e5bec8ef8d530cb86df028fefdcbacfa8b1db.png)  
^ Or this

Thanks!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 16, 2023, 4:57pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/2 "2023-03-16T16:57:50Z")

</div>

Here one example:

```julia
julia> using Plots, KernelDensity

julia> data = [randn(1000) for _ in 1:10]; # data for each coordinate

julia> densities = [kde(dat).density for dat in data]; # densities 

julia> density_matrix = hcat(densities...); # turn all into a matrix

julia> surface(density_matrix) # create surface

```

gives you:

![image](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f433b6cfd176d6ba512a37e04b068053365f098.png)

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 20, 2023, 12:18pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/3 "2023-03-20T12:18:59Z")

</div>

Had to work a little due to Julia being outdated, but here I am. It works, thanks! 🎉

Now I have a problem tho: axis don’t move with GR. What I mean is that if I tilt the `camera` so to look at the plot from the “left”, the axis don’t move and get covered:

```julia
    plot(density_matrix, st = :surface, xlabel="Binary weight", ylabel="Power",
        zlabel="Density",
        zlims=(0,200),
        c=cgrad(:heat, scale = :exp),
        camera = (-45,50)
    ) # create surface
    gui()

```

![post](https://global.discourse-cdn.com/julialang/original/3X/4/3/43ca2acdebc20dd904bf2c9b59368fe75db44440.png)

How can I move axis and labels so that they are visible?

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [March 20, 2023, 12:37pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/4 "2023-03-20T12:37:17Z")

</div>

I don’t know if this will be helpful, but I did this several years ago:

![image](https://global.discourse-cdn.com/julialang/original/3X/f/6/f6018b853644499fb6798388720936c334f11156.png)

The code is here: [GitHub - mthelm85/regression-plot: Cool 3D waterfall/scatter plot for regression](https://github.com/mthelm85/regression-plot)

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 20, 2023, 12:50pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/5 "2023-03-20T12:50:18Z")

</div>

That’s wonderful, thanks! I think I’ll tweak it a bit tho, right now I don’t want to fit a gaussian, but I want to visualize/verify the dispersion/residuals, so I think I’m going for a kde here

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 20, 2023, 12:51pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/6 "2023-03-20T12:51:15Z")

</div>

For completeness, here is another example:

[https://kristofferc.github.io/PGFPlotsX.jl/v1/examples/gallery/#D-Waterfall](https://kristofferc.github.io/PGFPlotsX.jl/v1/examples/gallery/#D-Waterfall)

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 20, 2023, 3:43pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/7 "2023-03-20T15:43:33Z")

</div>

Maybe this example is also useful: [How to produce a waterfall plot in Julia? - #3 by sijo](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441/3)

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [March 20, 2023, 8:11pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/8 "2023-03-20T20:11:14Z")

</div>

See also my answer (the last one) to this question: [Multiple normal distributions in one 3D plot](https://discourse.julialang.org/t/multiple-normal-distributions-in-one-3d-plot/95969)

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 20, 2023, 10:36pm UTC](https://discourse.julialang.org/t/how-can-i-combine-different-density-plots-in-a-3d-plot/96194/9 "2023-03-20T22:36:15Z")

</div>

Thanks to all for the interesting answers, amazing community as usual ❤.  
I’ll mark this as solved since I got an answer to the title question, then I’ll open a new thread for the axis issue.
