Histogram Bin Width Control With Plots.jl

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:

histogram(my_array, binwidth = 1)

Thank you in advance

Maybe this: How can I get the same bin size for both groups in a histogram? (Julia) - Stack Overflow

2 Likes

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

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

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

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

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))
1 Like

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

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

1 Like