how to color and label only certain histogram bars?
조회 수: 55 (최근 30일)
이전 댓글 표시
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
Adam Danz
2021년 6월 8일
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.
답변 (1개)
Joseph Cheng
2021년 6월 9일
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
Joseph Cheng
2021년 6월 9일
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
2021년 6월 9일
편집: Adam Danz
2021년 6월 9일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!