How to add error bars to multiple axes-plots?
Show older comments
I've produced a plot with 3 y-axes (for y1,y2,y3) and trying to add errorbars to the 3 line graphs. Initially I used 'plot' function as below (only to plot the data without error bars) and it went alright.
y1plot=plot(x,y1,'color',[0.8500,0.3250,0.0980]);
y2plot=plot(x,y2,'-b');
y3plot=plot(ax2,x,y3,'-k');
But when I replace 'plot' with 'errorbar' function as below, the graphs won't show up.
y1plot=errorbar(x,y1,y1_err,'color',[0.8500,0.3250,0.0980]);
y2plot=errorbar(x,y2,y2_err,'-b');
y3plot=errorbar(ax2,x,y3,y3_err,'-k');
Using 'plot' first, and then 'errorbar' as below also mess up the entire plot.
y1plot=plot(x,y1,'color',[0.8500,0.3250,0.0980]);
errorbar(y1,y1_err);
Maybe as it's being plotted on multiple axes, there's something going wrong which I find difficult to figure out. Any suggestions please! (The code is provided below for reference)
(R2020b)
-Thank you-
%reshape 4D arrays of data to 2D arrays
y1=reshape(A,1,12);
y2=reshape(B,1,12);
y3=reshape(C,1,12);
%reshape 4D arrays of error to 2D arrays
y1_err=reshape(A_err,1,12); %y1
y2_err=reshape(B_err,1,12); %y2
y3_err=reshape(C_err,1,12); %y3
%set x axis
x=datetime(2002,1:(16*12),1,'Format','MMM-yyyy');
%plot y1,y2
figure
ax1=axes;
yyaxis left;
y1plot=errorbar(x,y1,y1_err,'color',[0.8500,0.3250,0.0980]); %y1 on left
ax1.YColor=[0.8500,0.3250,0.0980];
ylabel('y1');
ax1.XTickMode='manual';
ax1.YTickMode='manual';
ax1.YLim=[min(ax1.YTick), max(ax1.YTick)];
ax1.XLimMode='manual';
grid(ax1,'on')
ytick=ax1.YTick;
yyaxis right
y2plot=errorbar(x,y2,y2_err,'-b'); %y2 on right
ax1.YColor='b';
ylabel('y2');
%create 2nd transparent axes & plot y3
ax2 = axes('position',ax1.Position);
y3plot=errorbar(ax2,x,y3,y3_err,'-k'); %y3 on left
ylabel('y3');
ax2.Color='none';
grid(ax2,'on');
ax2.YAxis.TickLabelFormat='%.2f';
%horizontally scale the y axis to align the grid
ax2.XLim=ax1.XLim;
ax2.XTick=ax1.XTick;
ax2.YLimMode='manual';
y1=ax2.YLim;
ax2.YTick=linspace(y1(1),y1(2),length(ytick));
%horzontally offset y tick labels
ax2.YTickLabel=strcat(ax2.YTickLabel,{' '});
Accepted Answer
More Answers (0)
Categories
Find more on Data Distribution Plots 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!


