필터 지우기
필터 지우기

Set Face Color of Bars in Bar Graph?

조회 수: 59 (최근 30일)
AmericanExpat26
AmericanExpat26 2017년 4월 15일
댓글: Star Strider 2017년 4월 15일
I am trying to set the face color of all of the bars of a bar graph in MATLAB while using the 'histc' option to have them all line up, so the trends I am trying to highlight can easily be observed. However, when trying 5 different ways to set the color of the bars, it always comes up as the same color (picture attached). The plotting code using an arbitrary x-y combination is:
bar(x,y,'histc');
To set the color to red, I have tried:
bar(x,y,'histc','r');
bar(x,y,'r','histc');
bar(x,y,'histc','FaceColor','r');
b = bar(x,y,'histc'); b(1:end).FaceColor = 'red'
b = bar(x,y,'histc'); set(b,'FaceColor','r');
None of the above changes the face colors of the bars to red. The full code I am using that generates the graph is attached. Any help with this would greatly appreciated as it seems it should be something pretty simple to do.
Full Code:
figure(1)
bar(datenumber,deltapHie,'histc');
hold on
zeroline = refline(0,0);
hold off
set(zeroline,'Color','k','LineWidth',1.4);
ylim([-0.96 0.36]);
set(gca,'FontSize',11);
set(gca,'ytick',[-0.9 -0.75 -0.6 -0.45 -0.3 -0.15 0 0.15 0.3]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.2f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('\DeltapH^{INT-EXT}_{final}','FontSize',12,'FontWeight','bold');
date_frmt = 'dd-mmm-yyyy';
beginTime = '27-Jan-2016';
endTime = '11-Feb-2016';
xlim([datenum(beginTime,date_frmt) datenum(endTime,date_frmt)]);
xticks(736356:3:736371);
dateformat = 1;
datetick('x',dateformat,'keeplimits','keepticks');
xlabel('Date','FontSize',12,'FontWeight','bold');
box on

답변 (1개)

Star Strider
Star Strider 2017년 4월 15일
What version of MATLAB are you using?
In R2017a, this works perfectly for me:
x = datenum(now) - [1:500];
y = randn(1, 500);
figure(1)
hb = bar(x, y, 'histc');
set(hb, 'FaceColor','r', 'EdgeColor','r')
datetick('x', 'dd-mmm-yyyy', 'keeplimits', 'keepticks')
I don’t see any problems in your code. It could be that your bars ar so narrow that you’re not seeing the 'FaceColor' but only 'EdgeColor'. See if adding 'EdgeColor','r' makes a difference.
  댓글 수: 2
Image Analyst
Image Analyst 2017년 4월 15일
Or 'edgeColor', 'none'
Star Strider
Star Strider 2017년 4월 15일
That would would work.
It depends what OP wants.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by