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

Hi all!

I’m analyzing some data that follows a more-or-less-linear dependency. Here’s a picture:

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

^ This, but of course “following” my data

^ Or this

Thanks!

Here one example:

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

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:

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

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

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

3 Likes

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

1 Like

For completeness, here is another example:

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

1 Like

Maybe this example is also useful: How to produce a waterfall plot in Julia? - #3 by sijo

See also my answer (the last one) to this question: Multiple normal distributions in one 3D plot

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.