How can I add error bars to a stacked plot?

11 vues (au cours des 30 derniers jours)
Arisha F
Arisha F le 24 Fév 2023
Commenté : Les Beckham le 24 Fév 2023
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

Réponse acceptée

Star Strider
Star Strider le 24 Fév 2023
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 commentaires
Star Strider
Star Strider le 24 Fév 2023
As always, my pleasure!
Les Beckham
Les Beckham le 24 Fév 2023
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Errorbars dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by