# Plot 2D histogram of 3D+ multivariate data

**URL:** <https://discourse.julialang.org/t/plot-2d-histogram-of-3d-multivariate-data/65525>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [July 29, 2021, 9:17pm UTC](https://discourse.julialang.org/t/plot-2d-histogram-of-3d-multivariate-data/65525 "2021-07-29T21:17:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [July 29, 2021, 9:17pm UTC](https://discourse.julialang.org/t/plot-2d-histogram-of-3d-multivariate-data/65525/1 "2021-07-29T21:17:21Z")

</div>

To plot a bivariate histogram with as many bins as I like in each dimension, I can use

```julia
using StatsBase, Plots
d1 = randn(10_000)
d2 = randn(10_000)

nbins1 = 25
nbins2 = 10
	
hist = fit(Histogram, (d1,d2),
		(range(minimum(d1), stop=maximum(d1), length=nbins1+1),
		range(minimum(d2), stop=maximum(d2), length=nbins2+1)))
plot(hist)

```

How can I make a 3D+ histogram object and extract bivariate slices for plotting? The plot step doesn’t work, but is there something similar to the following that would let me plot a bivariate histogram in dims 1 and 3?

```julia
d1 = randn(10_000)
d2 = randn(10_000)
d3 = randn(10_000)

nbins1 = 25
nbins2 = 10
nbins3 = 15
	
hist = fit(Histogram, (d1,d2,d3),
		(range(minimum(d1), stop=maximum(d1), length=nbins1+1),
		range(minimum(d2), stop=maximum(d2), length=nbins2+1),
		range(minimum(d3), stop=maximum(d3), length=nbins3+1)))
plot(hist[1,3])

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 29, 2021, 10:02pm UTC](https://discourse.julialang.org/t/plot-2d-histogram-of-3d-multivariate-data/65525/2 "2021-07-29T22:02:09Z")

</div>

The following seems to slice it, but please further QC its correctness:

```julia
for j in 1:nbins2
    x = getindex(hist.edges, 3)
    x = (x[1:end-1] + x[2:end])/2
    y = getindex(hist.edges, 1)
    y = (y[1:end-1] + y[2:end])/2
    z = hist.weights[:,j,:]
    display(heatmap(x,y,z, title="(i, j=$j, k)"))
    println("[ENTER] to continue...")
    readline()
end

```
