How would you plot a 3d histogram (or lego plot as people call it)

Hi how’s it going?

I want to make a 3d histogram, or a lego plot. What would be the best way of going about this?

For a hypothetical example, say x = [1,2,3,4,5], y = [0,5,30,10,15] and z = [50,100,150,200,250]

Here is a picture of what I’m looking for.

lego

I see you are using ROOT, my how that brings back memories.

The Plots.jl package wraps lots of other plotting libraries, so I’d imagine there is this type of 2D (i.e. the distribution is 2D) histogram somewhere in there, but it might take a lot of digging through documentation to find it.

One thing that occurs to me is that you can make something like this using surface in Makie. Of course, you’d need a separate histogram implementation to do that. There is one available in StatsBase.jl.

It doesn’t plot in 3D, but there’s histogram2d in StatsPlots.

using StatsPlots
x = randn(1000)
y = randn(1000)
histogram2d(x, y)

And while it isn’t quite the “Lego” effect, you can also fit a 2D histogram and plot it as a wireframe:

using StatsBase, Plots
h = fit(Histogram, (x, y), nbins=20)
plot(h) # same as histogram2d
wireframe(midpoints(h.edges[1]), midpoints(h.edges[2]), h.weights)

2 Likes

thank you!

In case it wasn’t apparent, my suggestion was the same as @ElOceanografo’s, surface in Makie is just an alternative that gives basically the same thing (I think makie has wireframe as well).

See this GMT.jl example