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

**URL:** <https://discourse.julialang.org/t/how-would-you-plot-a-3d-histogram-or-lego-plot-as-people-call-it/38427>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [April 29, 2020, 2:28pm UTC](https://discourse.julialang.org/t/how-would-you-plot-a-3d-histogram-or-lego-plot-as-people-call-it/38427 "2020-04-29T14:28:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [April 29, 2020, 2:28pm UTC](https://discourse.julialang.org/t/how-would-you-plot-a-3d-histogram-or-lego-plot-as-people-call-it/38427/1 "2020-04-29T14:28:51Z")

</div>

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](https://global.discourse-cdn.com/julialang/original/3X/0/0/00ea8cff64b5af36040ecd617a9198368889e2ad.png)

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 29, 2020, 2:36pm UTC](https://discourse.julialang.org/t/how-would-you-plot-a-3d-histogram-or-lego-plot-as-people-call-it/38427/2 "2020-04-29T14:36:21Z")

</div>

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](https://github.com/JuliaPlots/Makie.jl). Of course, you’d need a separate histogram implementation to do that. There is [one available](https://juliastats.org/StatsBase.jl/stable/empirical/) in StatsBase.jl.

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [April 29, 2020, 4:58pm UTC](https://discourse.julialang.org/t/how-would-you-plot-a-3d-histogram-or-lego-plot-as-people-call-it/38427/3 "2020-04-29T16:58:50Z")

</div>

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

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

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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/8/482c4e27b9c978d284585fe526c5074cdaf7eaa5.png)

---

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [April 29, 2020, 6:11pm UTC](https://discourse.julialang.org/t/how-would-you-plot-a-3d-histogram-or-lego-plot-as-people-call-it/38427/4 "2020-04-29T18:11:47Z")

</div>

thank you!

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 29, 2020, 6:29pm UTC](https://discourse.julialang.org/t/how-would-you-plot-a-3d-histogram-or-lego-plot-as-people-call-it/38427/5 "2020-04-29T18:29:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [April 29, 2020, 9:46pm UTC](https://discourse.julialang.org/t/how-would-you-plot-a-3d-histogram-or-lego-plot-as-people-call-it/38427/6 "2020-04-29T21:46:14Z")

</div>

See this [GMT.jl example](https://www.generic-mapping-tools.org/GMT.jl/latest/gallery/scripts_agu/bars3_peaks/)
