How can I plot a bar chart with 4 different y-axis and standard deviation?
7 views (last 30 days)
Show older comments
Hello, I would like to have a bar plot with 4 different categories. Each has got its own y-axis, as shown in the attached picture.

I figured out that there is a possibility to add mutlitple axis to a plot (addaxis). I'm using MATLAB R2014a, but I have access to R2017a at my university, if needed. This is my current code, based on this example:
function[]=test_multiple_axis_2()
w=[1.0,2.5,3.9,9.8,5.5,6.6,8.8,5.4,7.4,6.5];
x=[11.1,21.5,23.7,19.8,14.7,15.6,12.7,13.8,14.9,11.4];
y=[15.7,16.8,25.9,18.6,16.9,20.4,23.8,19.9,17.8,21.8];
z=[21.5,26.8,17.7,18.9,16.5,25.6,29.3,18.9,26.8,24.5];
std_w=[0.7,1.7,1.7,4.5,0.1,4.8,1.2,4.1,1.4,1.0];
std_x=[2.2,5.4,3.3,5.2,1.9,4.8,5.4,3.2,4.7,6.2];
std_y=[6.2,2.5,2.6,3.9,7.8,4.1,5.6,4.2,6.6,7.1];
std_z=[8.7,5.4,6.5,7.4,2.3,2.2,1.9,9.2,1.7,7.5];
hb = bar([1 2 3 4 5 6 7 8 9 10],[w' x' y' z'])
legend('w','x','y','z');
set(gca, 'FontSize',12,'XTick',[1 2 3 4 5 6 7 8 9 10],'XTickLabel',{'A','B','C','D','E','F','G','H','I','J'});
ylabel('unit w')%Here should be the addaxis code
errbar = [std_w; std_x; std_y; std_z]; % CREATE ‘errbar’ MATRIX
yd = [w' x' y' z']';
keyboard %i didn't understand the following code
hold on
for k1 = 1:3
errorbar([1:3]+.22*(k1-2), yd(k1,:), errbar(k1,:), '.k', 'LineWidth',2)
end
hold off
return
1 Comment
dpb
on 8 Jun 2017
Well, the FEX submission wraps the additional axes into a plot workalike; it doesn't handle different plotting styles as written.
I suppose you could probably fake it to create the axes with NaN for data that wouldn't show, saving the axes handles which appear to be returned. Then, hold on for each axis and a call to bar for each.
NB: to get the stacked bar this way on different axes you would have to use the "trick" I outlined in a couple of those earlier posts about barplots of making dummy N-column arrays where each are (N-1) NaN values excepting the one to be plotted on the particular axes. Without this, the x-axes will always be simply the one bar and each group will overlay the others.
Answers (1)
dpb
on 9 Jun 2017
OK, I ran your function -- it gets thru the barplot ok but barfs on the errorbar with size mismatch. Wasn't sure what your intent was so didn't look at that much at all... but, if the idea is to plot on the main axes with data scaled to that axis for proper relative height and then adjust the other axes limits to show actual value, then it's not too bad...
I just used default axes position for the first and adjusted the other positions based on it; it may be useful to toy with the actual sizes some but the idea is pretty simple; just a little arithmetic to adjust the positions of the axes--
figure
hAx=axes;
pos=get(hAx(1),'position');
posLeft=pos(1);
pos(1)=pos(1)*2; pos(3)=pos(3)-pos(1);
set(hAx(1),'position',pos)
pos(1)=posLeft; pos(3)=0.01;
hAx(2)=axes('position',pos,'color','none');
pos=get(hAx(1),'position');
hAx(3)=axes('position',pos, ...
'YAxisLocation','right','color','none');
pos(1)=posLeft+pos(1)+pos(3); pos(3)=0.01;
hAx(4)=axes('position',pos, ...
'YAxisLocation','right','color','none');
arrayfun(@ylabel,hAx.',char('X','W','Y','Z'))
results in

See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!