3 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

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

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

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!