plotting confidence interval and creating second axis in one plot

I would like to plot two means with their 95% confidence interval around each mean, plus the mean difference including confidence interval in the same plot but on a second axis on the right starting with 0 on the same level as the smaller mean value of the first two means. I came so far as to make the confidence intervals with the errorbar function but cannot figure out how to set a second axis and adjust it to start at the height of the one mean.
I attached a file with an example of how the plot should look like at the end. Thanks for any help!
plot([0 1 2 3],[0,mean_pre2,mean_post1,0],'bo')
hold on
plot([4 5],[mean_diff, 0],'ro')
U = [0,CIpre2_up,CIpost1_up,0,CIdiff_up,0];
L = [0,CIpre2_lo,CIpost1_lo,0,CIdiff_lo,0];
errorbar((0:5),[0,mean(maxAnklePower_pre2),mean(maxAnklePower_post1),0,mean(maxAnklePower_pre2-maxAnklePower_post1), 0],L,U,'o')

5 Comments

Have a look at the help and documentation for plotyy - it is a bit cludgy when it comes to which axes are the current one after the call, but should be the function you look for.
I would recommend yyaxis over plotyy if you're using Matlab R2106a or later.
@Adam Danz, thanks to you Adam I learnt something today (to people saying that I'm a slow learner, I say "PHU!" - this was only 5-6 years...)
Ha! I'd say you're a quick learner. You just discovered yyaxis and you were already able to address the OP's follow-up comment below my answer.
I frequently learn of long existing functions that I should have been using. The accumulation with every release is getting harder to keep up with.
@Adam Danz - this is especially so for problems where one has a "kind of working solution" - then it is not as urgent to keep track of the newer better solutions...

Sign in to comment.

 Accepted Answer

Here's a demo that should get you started.
The alignment of y=0 on the right axis with the first data point at "x" is done at the bottom half of the block below.
data = [3.2 5.2 5];
err = [1.5 .8 1.6];
yyaxis left
ax = gca();
errorbar(ax, data, err, 'ko','LineStyle','none', ...
'MarkerFaceColor','k')
set(ax,'xtick',1:3,...
'xticklabel',{'x','y','difference'},...
'xlim',[0.5, 3.5], 'ylim', [-1,8])
ax.XAxis.Color = 'k';
ax.YAxis(1).Color = 'k';
ax.YAxis(2).Color = 'k';
ylabel('Mean')
% Align 0 on right y-axis with the 'x'datapoint
% at the same scale as the left y-axis
yyaxis right
distanceToLeftAxLims = ax.YAxis(1).Limits - data(1);
ax.YAxis(2).Limits = distanceToLeftAxLims;
% Add horizontal reference line
yline(0, 'k--')

4 Comments

Thanks! This is great! Now the only thing missing is that the difference data is displayed according to the right axis and not the left. I did that but now the data point is always on the x-axis together with data point 'pre2' instead off 'difference'. Can I write a command to set the x-value of the third data point to 3?
yyaxis left
ax = gca();
errorbar(ax, data(1:2), err(1:2), 'ko','LineStyle','none', 'MarkerFaceColor','k')
set(ax,'xtick',1:3,...
'xticklabel',{'Pre2','Post1', 'Difference'},...
'xlim',[0.5, 3.5], 'ylim', [0,5])
ax.XAxis.Color = 'k';
ax.YAxis(1).Color = 'k';
ax.YAxis(2).Color = 'k';
ylabel('Mean')
% Align 0 on right y-axis with the 'x'datapoint
% at the same scale as the left y-axis
yyaxis right
ax = gca();
errorbar(ax, data(3), err(3), 'ko','LineStyle','none', 'MarkerFaceColor','k')
distanceToLeftAxLims = ax.YAxis(1).Limits - data(1);
ax.YAxis(2).Limits = distanceToLeftAxLims;
% Add horizontal reference line
yline(0, 'k--')
If I use
hold on
plot (3,data(3))
instead of the errorbar function, then the data point is not displayed in the graph at all.
A modification along these lines might help:
clf
yyaxis left
ax = gca();
errorbar(ax, 1:2,data(1:end-1), err(1:end-1), 'o','LineStyle','none', ...
'MarkerFaceColor',get(gca,'YColor'),'color',get(gca,'YColor'))
axis([0 4 -2 5])
yyaxis right
errorbar(ax, 3,data(end), err(end), 'o','LineStyle','none', ...
'MarkerFaceColor',get(gca,'YColor'),'color',get(gca,'YColor'))
set(gca,'XTick',[1 2 3],'XTickLabel',{'x','y','err'})
yyaxis left
That will put control back to the left axis.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D 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!