# Plotting 2D histogram with different bins for each dimension

**URL:** https://discourse.julialang.org/t/plotting-2d-histogram-with-different-bins-for-each-dimension/1790
**Category:** General Usage
**Tags:** plotting, statsplots
**Created:** [January 30, 2017, 8:42pm UTC](https://discourse.julialang.org/t/plotting-2d-histogram-with-different-bins-for-each-dimension/1790 "2017-01-30T20:42:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rwgardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwgardner/32/10344_2.png) [@rwgardner](https://discourse.julialang.org/u/rwgardner)
#### Post date: [January 30, 2017, 8:42pm UTC](https://discourse.julialang.org/t/plotting-2d-histogram-with-different-bins-for-each-dimension/1790/1 "2017-01-30T20:42:40Z")

</div>

I’m trying to plot a 2D histogram where the 2 dimensions have different bins.

I’m currently using

`marginalhist(x, x, fc=:plasma, bins=[0.0, 1.0, 2.0], legend=true)`

from PlotRecipes (although I’m not tied to that).

The problem is that it uses the same bins for both dimensions. How can I specify different bins for the second dimension? (Note that the documentation generally shows one setting the number of bins rather than the actual bin boundaries. I need to specify the bin boundaries. They are not evenly spaced.)

As a second question, anyone know how to label the bin values on the axes? As is, I get about 3 labels for all of the many bins.

Is there any plotting documentation I’m missing? Creating basic plots is explained but finding details for refining them is difficult, and the source, at least for PlotRecipes, is hard to read in many cases because functions are often created with macros.

Thanks.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [January 31, 2017, 7:21am UTC](https://discourse.julialang.org/t/plotting-2d-histogram-with-different-bins-for-each-dimension/1790/2 "2017-01-31T07:21:47Z")

</div>

You should be able to pass a Tuple of bins to `histogram2d` which is the Plots.jl recipe that does the work of `marginalhist`. This does not work at the moment, which looks like a bug to me. Could you open an issue on Plots.jl?  
AFAICS Plots.jl does not support arbitrary sized bins - are you seeking to get a plot where some bins are visibly larger than others? I cannot imagine the use case (it may still be 100% valid of course).

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [January 31, 2017, 7:44am UTC](https://discourse.julialang.org/t/plotting-2d-histogram-with-different-bins-for-each-dimension/1790/3 "2017-01-31T07:44:52Z")

</div>

Or you can do it manually with heatmap, the Plots code is really simple (slightly modified here):

```julia
function althist(x, y, xedges, yedges) 
   counts = zeros(length(yedges)-1, length(xedges)-1)

    for i=1:length(x)
        r = Plots.bucket_index(y[i], yedges)
        c = Plots.bucket_index(x[i], xedges)
        counts[r,c] += 1.0
    end

    heatmap(counts)
end

# An example of using the function
x = randn(1000); y = randn(1000)
xedges = linspace(minimum(x), maximum(x), 40)
yedges = linspace(minimum(y), maximum(y), 60)
althist(x, y, xedges, yedges)

```

Axis labelling is handled as normal in Plots - see e.g. `ticks` [https://juliaplots.github.io/attributes/](https://juliaplots.github.io/attributes/)

(note though that `heatmap` requires all bins on an axis to visually have the same width on the plot)

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [February 26, 2017, 7:51pm UTC](https://discourse.julialang.org/t/plotting-2d-histogram-with-different-bins-for-each-dimension/1790/4 "2017-02-26T19:51:14Z")

</div>

Even though the OP never responded I will just update for future readers.  
There was a bug in marginalhist that has been fixed. It is possible to specify different bin numbers and uneven bins (even though that is a tricky thing to do on a 2d-histogram as larger bins will get more counts).

```julia
using StatPlots
pyplot() # currently the only backend who supports display of heatmaps with uneven bin sizes.
x, y = randn(10000), randn(10000)
marginalhist(x, y, fc = :plasma, bins = (6, 6sort(rand(20))-3))

```

giving

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

---

<div class="post-metadata">

### Author: ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)
#### Post date: [November 2, 2024, 11:11am UTC](https://discourse.julialang.org/t/plotting-2d-histogram-with-different-bins-for-each-dimension/1790/5 "2024-11-02T11:11:22Z")

</div>

I think it still works, but you have to use `StatsPlots` instead of `StatPlots`
