Plot a histogram of specific bin width

I have a 32-bit image stack of which I wish to create a histogram. This work well by simply using
run("Histogram", "bins=256 use y_max=Auto stack");

However, I wish to fix the bin width to a certain value. By default I believe its set to (min-max)/nBins, but I would like to fix it to a specific value.Is there a way to integrate that in a macro?

1 Like


Perhaps you will find this example macro for custom histograms helpful? You can always check the Built-In ImageJ function list as well.

eta :slight_smile:

@etadobson this explains how to set the number of bins, but not how to manually specify bin width, no?

@imagejan You are correct !!! Oops!!! :wink: That’s embarrassing …

Hi @etadobson, thanks for that quick reply!
Indeed @imagejan is correct, I am looking for a way to manually specify the bin width :slight_smile:

Nevertheless, the CustomHistogram.txt macro suggested by @etadobson contains all you need to manually specify the bin width, by defining all of bins, x_min and x_max. To achieve a bin width of two you can do for example:

run("M51 Galaxy (177K, 16-bits)");
setMinAndMax(0, 1024); // the current display doesn't affect the histogram
run("Histogram", "bins=256 x_min=0 x_max=512");

For even more control, or if you want to extract certain values directly via the API, switch to a scripting language such as Groovy:

#@ ImagePlus imp // take e.g. M51 Galaxy sample image

import ij.process.StackStatistics

simpleStats = new StackStatistics(imp)

println "Automatic bin size: ${simpleStats.binSize}"
println "Automatic hist min: ${simpleStats.histMin}"
println "Automatic hist max: ${simpleStats.histMax}"

binWidth = 2
min = 0
max = 512
customStats = new StackStatistics(imp, (max-min)/binWidth as int, min, max)

println "Custom bin size: ${customStats.binSize}"
println "Custom hist min: ${customStats.histMin}"
println "Custom hist max: ${customStats.histMax}"

which will give (for the M51 Galaxy sample image):

Automatic bin size: 39.4765625
Automatic hist min: 0.0
Automatic hist max: 10106.0
Custom bin size: 2.0
Custom hist min: 0.0
Custom hist max: 512.0

Awesome, thanks! I realized something similar this morning…and looking back at my original post, I think that it actually already contains the answer

thanks also the groovy script!

1 Like

You are great @imagejan !!! :slight_smile:



Great work all.

I have a similar problem that you might be able to help me with.

I have stacks of images from samples that I want to produce histograms for. The greyscale values start at 0 and go up to approximately 700 (it’s different for each sample). Therefore I need to bin width to be 1, or the number of bins to be the same as the maximum greyscale value, as it will be different for each sample I want to analyse.

Is there a way to edit the custom histogram macro so that it will change the bin width to one?


Good day Rachel,

if you have images with gray-values going up to about 700, these must be either 32bit or 16bit images. Please tell us what kind of images you are going to analyze.



Hi Herbie,

They are all 16bit.


How about changing

binWidth = 1

in the Groovy script I posted above?

1 Like

Here is a rather concise ImageJ-macro that should give you the desired histogram for a single image or slice:

getMinAndMax(min, max);
run("Histogram", "bins="+max+" x_min=0 x_max="+max);

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.

If you want a histogram for every slice you need to loop over all slices.



1 Like

if you alternatively want one histogram per stack, and the maximum value to be the maximum contained across the entire stack, then you can use similarly to @anon96376101 solution:

Stack.getStatistics(voxelCount, mean, min, max, stdDev);
run("Histogram", "bins="+max+" x_min=0 x_max="+max+" stack");

@CellKai that works perfectly thank you all so much for your help!


1 Like

2 posts were split to a new topic: Macro for computing histogram of a stack