how to color and label only certain histogram bars?
53 views (last 30 days)
Show older comments
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
3 Comments
Adam Danz
on 8 Jun 2021
You either need to make two histograms on the same plot and color them differently or use bar() with a width of 1 to create a bar plot where you can control the color of each bar.
Answers (1)
Joseph Cheng
on 9 Jun 2021
y = randn(100);
[N X]=hist(y(:),10);
%plot all the data
figure,clf,hbar = bar(X,N,'b')
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')
% 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
3 Comments
Joseph Cheng
on 9 Jun 2021
Really histograms should have touching bars? i don't think i've ever noticed (maybe because of the number of bars they always look touching anyways) that based on the data i know its descrete or should be a continuous touching binning. for example for a continuous year that a non-touching bar histogram wouldn't exclude dec~january because of a slight gap (unless otherwise stated in the title). Though it makes some sense in the current state we're in of bad visuals to mislead people.
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.
Here's more info histograms-vs-bar plots: storytellingwithdata.com
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!