# How to set histogram alignment in Plots.jl

**URL:** <https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273>\
**Category:** Visualization\
**Created:** [June 14, 2017, 2:44pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273 "2017-06-14T14:44:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbeltagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbeltagy/32/130_2.png) [@mbeltagy](https://discourse.julialang.org/u/mbeltagy)\
**Post date:** [June 14, 2017, 2:44pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/1 "2017-06-14T14:44:12Z")

</div>

In matplotlib.pyplot.hist their is an _align_ keyword that changes alignment of the histogram from “mid”, “left”, or “right”. This keyword in currently not supported on Plots.jl. It there a way I could force pass it to the `pyplot` graphical backend?

---

<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:** [June 14, 2017, 2:58pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/2 "2017-06-14T14:58:07Z")

</div>

No, there is no way to pass backend-specific code through Plots. Plots calls are meant to be backend-agnostic.  
To me personally that option sounds strange, like it would misrepresent the data.  
If you want the behaviour you could just overload the ticks, e.g. `xticks = (1:5+0.5, 1:5)`

---

<div class="post-metadata">

**Author:** ![mbeltagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbeltagy/32/130_2.png) [@mbeltagy](https://discourse.julialang.org/u/mbeltagy)\
**Post date:** [June 14, 2017, 4:01pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/3 "2017-06-14T16:01:55Z")

</div>

Something seems quite wrong with way histogram on plots is working. I can not make it match what one gets from PyPlot. I just submitted an issue: [https://github.com/JuliaPlots/Plots.jl/issues/930](https://github.com/JuliaPlots/Plots.jl/issues/930)

---

<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:** [June 14, 2017, 7:09pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/4 "2017-06-14T19:09:40Z")

</div>

I find it interesting that you assume something is wrong in Plots because it has a different output than PyPlot does? Is PyPlot the reference for ultimate truth?

On a more serious note, there are many different algorithms for calculating bin numbers and edge positions. Most algorithms try to hit some edges that are easy to interpret, and a bin number that represents the distribution. Plots by default uses Sturges’ method, this was chosen for consistency with StatsBase.Histogram, but it is also the most widespread (it is the default in R). But there are a lot of other possibilities.

The binning in Plots is determined by the `bins` parameter. To see the default values and types, aliases and various options for plot attributes, you can use the `plotattr` function on an attribute (or any of its aliases):

```julia
julia> plotattr("bins")
bins {Integer, NTuple{2,Integer}, AbstractVector or Symbol}
bin, binss, nb, nbin, nbins

Default is :auto. For histogram-types, defines the approximate number of bins to aim for, or the auto-binning algorithm to use (:sturges, :sqrt, :rice, :scott or :fd). For fine-grained control pass a Vector of break values, e.g. `linspace(extrema(x)..., 25)`
Series attribute, default: auto

```

If you don’t like the result, try a different algorithm. If you want complete control over bins, pass an AbstractVector with edge positions. If you want exactly 30 bins, use `linspace(extrema(x)..., 30)`.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 14, 2017, 9:43pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/5 "2017-06-14T21:43:27Z")

</div>

Great answer @mkborregaard, thanks!

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 14, 2017, 9:45pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/6 "2017-06-14T21:45:21Z")

</div>

Is this kind of thing in the docs / Examples somewhere?

---

<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:** [June 15, 2017, 5:48am UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/7 "2017-06-15T05:48:33Z")

</div>

Thanks! The output of `plotattr` can also be found at [https://juliaplots.github.io/attributes/](https://juliaplots.github.io/attributes/) but I just checked and the current build of the webpage is from before @oschulz added the new and expanded histogram functionality to Plots.

One of the things that’s high on the prority list is to have a section in the docs for each seriestype, so there will be a page on histogram capabilities in the (hopefully near) future.

---

<div class="post-metadata">

**Author:** ![mbeltagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbeltagy/32/130_2.png) [@mbeltagy](https://discourse.julialang.org/u/mbeltagy)\
**Post date:** [June 15, 2017, 7:12am UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/8 "2017-06-15T07:12:08Z")

</div>

Thank you @mkborregaard. Far from thinking that PyPlot is the ultimate truth, I had assumed that Plots would be making use of the PyPlot’s histogram plotting function. Hence the confusion. Now I understand that it is doing its own thing, just working the bare primitives of the “backend”.

---

<div class="post-metadata">

**Author:** ![mbeltagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbeltagy/32/130_2.png) [@mbeltagy](https://discourse.julialang.org/u/mbeltagy)\
**Post date:** [June 19, 2017, 12:05pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/9 "2017-06-19T12:05:12Z")

</div>

@mkborregaard Somethings still seems completely non-intuitive about setting `bins`, this is why I created an issue about it.  
For example if you run:

```julia
using Distributions, Plots
pyplot()
srand(15); x=rand(Exponential(4),200);
for i=10:5:50
    histogram(x,bins=i)
    savefig("TestPlotFigPlts_$i.png")
end

```

The 10 and 15 bins plot will be identical. They will show 11 bars. Also the 20, 25, 30, and 35 bins will also be identical, so will the 40, 45, and 50 bins.

I would expect the number of bars to match the number of `bins`. This does not seem to be happening. I know that be exactly controlled via `linspace(extrema(x)..., real_bins)`

The closest I could get to perform as desired is to run

```julia
using Distributions, Plots
pyplot()
srand(15); x=rand(Exponential(4),200);
for i=10:5:50
    histogram(x,bins=linspace(extrema(x)..., i+1))
    savefig("TestPlotFigPlts_$i.png")
end

```

---

<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:** [June 19, 2017, 12:16pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/10 "2017-06-19T12:16:35Z")

</div>

I guess what is intuitive is different for different people. The advantage of this is that it’s versatile. If you want some specific bins or want several histograms to have the same bins, just define a vector with the edge positions and pass it to `bins`. If you do want to advocate to have it changed you should re-open the issue, but I strongly doubt most of JuliaPlots will agree with you - since we just had a 3-month discussion developing the current system.

---

<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:** [June 19, 2017, 12:38pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/11 "2017-06-19T12:38:49Z")

</div>

Let me just state what I mean - if you always expect the bin numbers to be exact, what should then constitute the minimum and maximum values? Then that is determined by the most extreme points, which means that the bin locations and sizes will never be consistent but go all over the place only determined by the (essentially random) extrema. To have any kind of consistency, you’ll need to specify the highest and lowest bin edge. That is, in fact exactly what you do when specifying `bins = linspace(-5,5,20)` (or -5:5 if you’re happy with integer bins), so that cannot be any more succinct than what it is now.  
Then you may wish to align the extrema to some sensible values (like integers), but then you should also align the edges to sensible values. That is in fact what the system does now - you get the exact number of bins, some of them towards the ends of the distribution are just empty and thus doesn’t show up! You cannot have all of these at once - exact bin numbers, guarantee for the edge-most bins to contain points, and stability to the outliers. That’s mathematically impossible.  
I should add that the functionality you showed for pyplot in the issue is highly unusual - some variant of what Plots does now is standard across statistical software.

---

<div class="post-metadata">

**Author:** ![mbeltagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbeltagy/32/130_2.png) [@mbeltagy](https://discourse.julialang.org/u/mbeltagy)\
**Post date:** [June 19, 2017, 12:59pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/12 "2017-06-19T12:59:27Z")

</div>

Thank you for clarifying the workings of `Plots.jl`’s `histogram` function. This makes a great deal of sense now. One is often biased by the tools that one uses. In my case, I haven’t explored much beyond `matplotib`, I was blind to richness of choices in the ways one can construct histograms. I now have a better appreciation of the limitations of `matplotlib`’s `hist` function.

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [May 3, 2019, 8:56am UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/13 "2019-05-03T08:56:10Z")

</div>

Just in case someone else find this doesn’t work in 1.0, since

```julia
julia> 1:5 + 0.5
1.0:1.0:5.0

```

one should write

```julia
xticks = (1:5) .+ 0.5, 1:5

```

in 1.0

---

<div class="post-metadata">

**Author:** ![abelborges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelborges/32/16695_2.png) [@abelborges](https://discourse.julialang.org/u/abelborges)\
**Post date:** [August 1, 2020, 2:47pm UTC](https://discourse.julialang.org/t/how-to-set-histogram-alignment-in-plots-jl/4273/14 "2020-08-01T14:47:11Z")

</div>

And if someone else is still looking into this topic, I’m talking from Julia 1.4.2 and Plots.jl 1.5.6 and I’ve just discovered that Freedman-Diaconis is the default now for the number of bins (idk since when). Personally, I find it much better than Sturges rule. That’s the post 😛
