# Histogram Bin Width Control With Plots.jl

**URL:** <https://discourse.julialang.org/t/histogram-bin-width-control-with-plots-jl/68063>\
**Category:** New to Julia\
**Tags:** plots\
**Created:** [September 12, 2021, 8:28pm UTC](https://discourse.julialang.org/t/histogram-bin-width-control-with-plots-jl/68063 "2021-09-12T20:28:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [September 12, 2021, 8:28pm UTC](https://discourse.julialang.org/t/histogram-bin-width-control-with-plots-jl/68063/1 "2021-09-12T20:28:29Z")

</div>

Hello,

I am using Plots.jl to make a histogram. I would like to have control over the bin size, but I can’t seem to find anything specifically saying how to do this in the documentation (or in other posts).

I have an array with all integer values in it, and I would like to create a histogram with bins with a width of exactly 1. I would expect that there is something similar to this that works:

```julia
histogram(my_array, binwidth = 1)

```

Thank you in advance

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 12, 2021, 8:51pm UTC](https://discourse.julialang.org/t/histogram-bin-width-control-with-plots-jl/68063/2 "2021-09-12T20:51:14Z")

</div>

Maybe this: [How can I get the same bin size for both groups in a histogram? (Julia) - Stack Overflow](https://stackoverflow.com/questions/65783708/how-can-i-get-the-same-bin-size-for-both-groups-in-a-histogram-julia)

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [September 12, 2021, 9:17pm UTC](https://discourse.julialang.org/t/histogram-bin-width-control-with-plots-jl/68063/3 "2021-09-12T21:17:32Z")

</div>

Thank you for the link! I managed to make it work for what I need to do. For future reference, something like that works:

```julia
data = rand(0:100, 10000) # 1D Array with data

histogram(data; bins = 0:1:100 ) # Creates the histogram

```

---

<div class="post-metadata">

**Author:** ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)\
**Post date:** [September 14, 2021, 8:09am UTC](https://discourse.julialang.org/t/histogram-bin-width-control-with-plots-jl/68063/4 "2021-09-14T08:09:49Z")

</div>

Have you tried the auto-binning algorithms( :sturges, :sqrt, :rice, :scott, :fd) ?  
In your sample “:sqrt”-shows the same histogram

```julia
using Plots
data = rand(0:100, 10000) # 1D Array with data
plot(
histogram(data; bins = 0:1:100 ), # Creates the histogram
histogram(data, bins=:sqrt);
layout=(2,1))

```

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [September 14, 2021, 8:20pm UTC](https://discourse.julialang.org/t/histogram-bin-width-control-with-plots-jl/68063/5 "2021-09-14T20:20:54Z")

</div>

That is useful in this specific case, thanks! But I guess I was looking for a more general way to control bin width.

---

<div class="post-metadata">

**Author:** ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)\
**Post date:** [September 16, 2021, 5:44am UTC](https://discourse.julialang.org/t/histogram-bin-width-control-with-plots-jl/68063/6 "2021-09-16T05:44:46Z")

</div>

In the" Images"-Packages are two functions “build\_histogram” and “imhist”  
An example with “imhist”

 ![img1](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a8615d41abd2ec71231253a06ba311ce535e702.webp)
