
How to adjust the color bar in an image histogram?
10 views (last 30 days)
Show older comments

Here's the code I am using:
imhist(img1_green)
xlabel('Pixel Intensity')
ylabel('Pixel Count')
title('Image 1 Green Histogram')
I cannot seem to show the xlabel in this image, and was wondering if the color gradient bar can be shifted.
0 Comments
Accepted Answer
Image Analyst
on 6 Sep 2021
Try this. Adapt as needed.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
rgbImage=imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(rgbImage);
subplot(2, 3, 1);
imshow(rgbImage);
axis('on', 'image');
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
subplot(2, 3, 4);
histogram(redChannel, 'FaceColor', 'r', 'EdgeColor', 'none');
grid on;
title('Red Channel Histogram', 'fontSize', fontSize);
ylabel('Pixel Count', 'fontSize', fontSize);
xlabel('Red Channel Gray Level', 'fontSize', fontSize);
subplot(2, 3, 5);
histogram(greenChannel, 'FaceColor', 'g', 'EdgeColor', 'none');
grid on;
title('Green Channel Histogram', 'fontSize', fontSize);
ylabel('Pixel Count', 'fontSize', fontSize);
xlabel('Green Channel Gray Level', 'fontSize', fontSize);
subplot(2, 3, 6);
histogram(blueChannel, 'FaceColor', 'b', 'EdgeColor', 'none');
grid on;
title('Blue Channel Histogram', 'fontSize', fontSize);
ylabel('Pixel Count', 'fontSize', fontSize);
xlabel('Blue Channel Gray Level', 'fontSize', fontSize);
g = gcf;
g.WindowState = 'maximized'

0 Comments
More Answers (1)
DGM
on 12 Apr 2025
If we are to answer the question directly:
% you have an RGB image
inpict = imread('peppers.png');
% imhist() cannot plot and return outputs in the same call,
% and either way, it cannot return axes or other object handles.
% so you just have to plot it ...
imhist(inpict(:,:,2))
% ... and then find the axes it created.
% there will be two, but only one is tagged.
% when imhist() exits, the current axes is the plot axes,
% but the lower axes is the colorstripe.
haxcs = findobj(gcf,'type','axes','tag','colorstripe');
% these need to go on the plot axes (which should be gca)
ylabel('Pixel Count')
title('Image 1 Green Histogram')
% and this needs to go on the colorstripe axes,
% otherwise it ends up buried under the colorstripe.
xlabel(haxcs,'Pixel Intensity')
% the colorstripe axes contains a line object and an image object.
% the image is a 1xNBINSx3 RGB image (a unit-scale gray ramp).
% we can replace it or modify it as needed
himg = findobj(haxcs,'type','image');
himg.CData(:,:,[1 3]) = 0; % just make it green
That said, I don't think it's really worth doing it that way. One option is as @Image Analyst shows. Just get the counts and use histogram().
If you do want the colorstripe like imhist() provides, or if you want to control other things like scale and bin alignment, consider that imhist()'s plotting functionality has limitations other than the slight inconveniences above.
The replacement tool described there (MIMT imhistFB()) supports the direct specification of the stem/bar color, and the colortable used for the colorstripe. That means you can more easily color-code your histogram plots. Furthermore, if you want histograms for all channels of a color image, consider MIMT cshist(). This builds on imhistFB() and allows the easy creation of histograms for images as represented in various color models.

0 Comments
See Also
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!