Set Face Color of Bars in Bar Graph?
59 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/162959/image.jpeg)
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
0 commentaires
Réponses (1)
Star Strider
le 15 Avr 2017
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 commentaires
Voir également
Catégories
En savoir plus sur Bar Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!