How can I add error bars to a stacked plot?

Trying to add errorbars to a stacked plot? Seems there's no way to do this which is a shame as I very much like the way a stacked plot presents the data by comparing 3 variables with distinctly different y-axes against my x variable.
figure()
s = stackedplot(dist, stack, "DisplayLabels", ylabels); % stacked plot
xlabel('Distance from Springhead (m)')
title('Carbonate Chemistry with Distance Downstream')
grid on

 채택된 답변

Star Strider
Star Strider 2023년 2월 24일
A (slightly tortured) tiledlayout array might work —
x = 0:0.5:10;
y = randn(3,numel(x));
err = randn(3,numel(x))/5;
figure
tiledlayout(3,1)
nexttile
errorbar(x, y(1,:), err(1,:))
Ax = gca;
Ax.XAxis.Visible = 'off';
% axis('padded')
nexttile
errorbar(x, y(2,:), err(2,:))
Ax = gca;
Ax.XAxis.Visible = 'off';
axis('padded')
nexttile
errorbar(x, y(3,:), err(3,:))
Ax = gca;
xt = Ax.XTick;
Ax.XAxis.Visible = 'off';
% axis('padded')
hold on
plot(xlim, [1 1]*min(ylim), '-k')
plot([1;1]*xt, [zeros(size(xt)); ones(size(xt))*0.07*diff(ylim)]+min(ylim),'-k')
hold off
text(xt, ones(size(xt))*min(ylim), string(xt), 'Horiz','center', 'Vert','top')
.

댓글 수: 3

Arisha F
Arisha F 2023년 2월 24일
Thanks that line turning the visible axis off saved me!
As always, my pleasure!
Note that you can adjust the spacing between the "stacked" plots to tweak how it looks (experiment with it).
Also, you should use (or not use) axis('padded') on all three tiles.
x = 0:0.5:10;
y = randn(3,numel(x));
err = randn(3,numel(x))/5;
figure
tiledlayout(3,1, 'TileSpacing', 'tight') %<<< or 'compact' or 'none' (but 'none' looks bad here)
nexttile
errorbar(x, y(1,:), err(1,:))
Ax = gca;
Ax.XAxis.Visible = 'off';
grid on
% axis('padded')
nexttile
errorbar(x, y(2,:), err(2,:))
Ax = gca;
Ax.XAxis.Visible = 'off';
% axis('padded')
grid on
nexttile
errorbar(x, y(3,:), err(3,:))
Ax = gca;
xt = Ax.XTick;
Ax.XAxis.Visible = 'off';
% axis('padded')
hold on
plot(xlim, [1 1]*min(ylim), '-k')
plot([1;1]*xt, [zeros(size(xt)); ones(size(xt))*0.07*diff(ylim)]+min(ylim),'-k')
hold off
text(xt, ones(size(xt))*min(ylim), string(xt), 'Horiz','center', 'Vert','top')
grid on

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Errorbars에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2023년 2월 24일

댓글:

2023년 2월 24일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by