# how to color and label only certain histogram bars?

조회 수: 88 (최근 30일)
YH 2021년 6월 8일
편집: Adam Danz 2021년 6월 9일
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이전 댓글 1개 표시이전 댓글 1개 숨기기
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.
YH 2021년 6월 9일
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
histogram(Mvalue,30);
hold on
histogram(hvalue,30,'FaceColor','red');
hold off

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Joseph Cheng 2021년 6월 9일
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
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
'HorizontalAlignment','center')
end
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
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일
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.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by