# how to color and label only certain histogram bars?

56 views (last 30 days)
YH on 8 Jun 2021
Edited: Adam Danz on 9 Jun 2021
Hello all,
I have a matrix 2*n . the first column represents the values and the second represets the time in years.
I want to creat a histogram of the values with GEV distribution fit, where only some bars are colored red (bars of values of certain years), then I want to put text labels above those red bars each with the corresponding year value.
would appreciate any tips :)
Best regards
YH on 9 Jun 2021
Thank you for your explaination.
I attached my data file as mat file
tried the following code but the histogram bars of the two histograms overlaps. how can I put the red bar over the blue for example? and how can I make them at the same edges?
I also want to name the years values of only the bar that are red colored
I tried many things, but seems like I am missing something
many thanks in advance!
histogram(Mvalue,30);
hold on
histogram(hvalue,30,'FaceColor','red');
hold off

Joseph Cheng on 9 Jun 2021
Here is an example for @Adam Danz said with using bar
y = randn(100);
[N X]=hist(y(:),10);
%plot all the data
figure,clf,hbar = bar(X,N,'b')
hbar =
Bar with properties: BarLayout: 'grouped' BarWidth: 0.8000 FaceColor: [0 0 1] EdgeColor: [0 0 0] BaseValue: 0 XData: [-3.2136 -2.4893 -1.7650 -1.0407 -0.3164 0.4079 1.1322 1.8565 2.5808 3.3051] YData: [25 157 612 1674 2717 2652 1508 527 114 14] Show all properties
stdval = std(y(:));
%isolate some values you want to highlight
hlight = find(abs(X)>stdval);
hold on
% add to the bar chart the isolated bars
hhibar = bar(X(hlight),N(hlight),'r')
hhibar =
Bar with properties: BarLayout: 'grouped' BarWidth: 0.8000 FaceColor: [1 0 0] EdgeColor: [0 0 0] BaseValue: 0 XData: [-3.2136 -2.4893 -1.7650 -1.0407 1.1322 1.8565 2.5808 3.3051] YData: [25 157 612 1674 1508 527 114 14] Show all properties
% add labels as shown
x = get(hhibar,'XData');
y = get(hhibar,'YData');
ygap = 0.1; % Specify vertical gap between the bar and label
ylimits = get(gca,'YLim');
set(gca,'YLim',[ylimits(1),ylimits(2)+0.2*max(y)]); % Increase y limit for labels
% Create labels to place over bars
for i = 1:length(x) % Loop over each bar
xpos = x(i); % Set x position for the text label
ypos = y(i) + ygap; % Set y position, including gap
htext = text(xpos,ypos,num2str(y(i))); % Add text label
set(htext,'VerticalAlignment','bottom',... % Adjust properties
'HorizontalAlignment','center')
end Adam Danz on 9 Jun 2021
Edited: Adam Danz on 9 Jun 2021
I'm assuming OP's x-data are continuous since it's apparently the result of a fit and fit-data are typically not categorical; also the OP mentioned years which is typically continuous but not always.
If data are plotted comparing the first year of every decade, for example, that should be a bar plot since the years are not continuous. If data consisting of daily rainfall between 1999 and 2020 are plotted in a histogram with year-bin-widths, that is continuous and should be plotted with a histogram. If a year of data are missing, the histogram would have a gap which tells the viewer that data is missing (or there was no rainfall). Gaps are informative in histograms. Gaps in bar plots are misleading if the data are continuous.
Also, bars are typically labeled with categorical labels centered under the bar. Histograms are typically labeled with bin-edges where each bar has a label that defines the left and right side of the bar. Sometimes labels can be centered leaving the reader to understand the bin edges.
Lastly, the width of a bar is irrelevant but the width of a histogram-bar carries meaning. It visually depicts the range of values along the x-axis which is particularly important when the bars have varying ranges.
Consider this image I picked quickly from the internet. Let's say the x-axis is age. We can clearly see there were more subjects between 30-50 than other age groups. This information is lost in bar plots.
Also, the total area of the histogram is proportional to the data (particularly clear with normalized data). These characteristics are not available in bar plots. 