필터 지우기
필터 지우기

How to change the color for bar graphs and create space between grouped bars?

조회 수: 10 (최근 30일)
Hi How to change the face color of the individual bar in a grouped bar chart. How to create space between different groups in a bar chart.
For example, as shown in the below image. I have 3 different category i.e. A,B,C and each category has 5 bars i.e. 2,5,10 20 and 25. How to create this kind of graph in matlab.
The data for A, B, C are... A= [10,13,9,6,20]' B= [2, 4, 7, 9,10]' C=[-2,-3,-8,-10,-11]'
Thanks

채택된 답변

dpb
dpb 2017년 7월 8일
편집: dpb 2017년 7월 8일
ABC=[ [10,13,9,6,20]' [2, 4, 7, 9,10]' [-2,-3,-8,-10,-11]'].';
hBar=bar(ABC,'grouped'); % basic grouped bar plot, keep handle
hFg=gcf; hAx=gca; % handles to current figure, axes
ylim([-20 30]) % space out a little extra room
hAx.XTickLabel={'A';'B';'C'}; % label x axis
hAx.YColor=get(hFg,'color'); box off % hide y axis/labels, outline
hLg=legend(num2str([2;5;10;20;25]), ...
'orientation','horizontal', ...
'location','north'); legend('boxoff') % add legend
yields
With more recent release than I have here (R2014b) you can also move the y-axis to the origin with
hAx.XAxisLocation='origin';
to essentially duplicate the example.
hBar(N).FaceColor='r';
would for example color the N th bar in the group red if don't like default colors (which, granted, aren't the most pleasing they could be).
ADDENDUM: OK, am back so to do error bars on the bar plot--
Start by combining the data into an array instead of using separate variables -- a general coding principle in Matlab, btw, makes things much more efficient as a rule.
ABCerr=[A_error;B_error;C_error];
x=repmat([1:size(ABC,1)].',1,size(ABC,2)); % bar x nominals
xOff=bsxfun(@plus,x,[hBar.XOffset]); % bar midpoints x
hold all % keep axis limits, etc.
hEB=errorbar(xOff,ABC,ABCerr,'.'); % add errorbars
Above will have a set of default colors for the errorbar series that won't necessarily coincide with the bar series already have.
I do not know where the default bar color series values are stored; the default 'FaceColor' value is the string 'Flat' which isn't of much help in matching to but as noted above you can reset it to either one of the eight defined color strings or any RGB triplet to match up with the default errorbar colors could be
for i=1:length(hBar)
hBar(i).FaceColor=hEB(i).Color;
end
or choose a set for both. Or, on further reflection and looking at the plot created, maybe 'k' for all the error bars might help them be visible, particularly the lower limits that are in the bars.
Another fixup would be
hBar(1).BaseLine.Color='k';
to show the baseline under the bars; it follows the y-axis color by default so became invisible when turned that color to match the figure background.
  댓글 수: 4
dpb
dpb 2017년 9월 15일
Again move Answer to Comment...dpb
Hi
How to turn those vertical bar graphs into horizontal bars keeping the same structure (same code as your first comment.
Thanks
dpb
dpb 2017년 9월 15일
For horizontal bar graph, use barh
Prior to release after R2014b (but undocumented as to when except perhaps in individual release notes) errorbar only supported vertical error bars; at some point w/ HG2 it added a new 'orientation' property.
Same ideas hold as before; simply swap the axes looking at for the specific properties as needed...

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

추가 답변 (2개)

R7 DR
R7 DR 2017년 7월 9일
Hi Thanks. Now its perfect :) :)
I still have one question. I am trying to give hatching lines in place of colors using the following file, but the quality of the image is very bad. http://se.mathworks.com/matlabcentral/fileexchange/26797-applyhatch-plusc
Could you please tell me how to give differnt hatching pattern on bar chart?
Thanks
  댓글 수: 1
dpb
dpb 2017년 7월 9일
편집: dpb 2017년 7월 9일
Another Matlab weakpoint -- there's no explaining TMW's failure to include multitude of hatching patterns in lo! these many years.
I've also tried a number of FEX hatching submissions and had only marginal success with any of them, sorry.
The ones I've used worked (to a lesser or greater degree of pleasing success) directly on patch object handles; I don't know about trying them with bar series objects.
In short, I don't know enough specifically to help; I'd suggest it's a big enough oversight to be worth a bug/feature report and enhancement request. I don't recall if I actually submitted one back when was playing with those routines or not; that was a couple years ago now...

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


Hunter Pruett
Hunter Pruett 2020년 5월 23일
편집: Hunter Pruett 2020년 5월 23일
Just finished the barmod function--controls relative position of just about everything in the plot. Hope this helps anyone else with this question--I was having similar issues and found the lack of functionality a bit disappointing. See the demo for color modification and such.

카테고리

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