Shade area under a plot with MATLAB

11 次查看(过去 30 天)
Gianmarco Broilo
Gianmarco Broilo 2021-11-17
回答: DGM 2021-11-18
I am trying to shade the area under a curve on MATLAB, this is the code I have:
The problem is that is shading only the negative part of the graph, not its symmetric positive part..
plot(Navg(2:end),closestapproach(2:end)./1e3-2634,'k')
hold on
plot(serrN_2000,closestapproach(2:end)./1e3-2634,'b');
hold on
plot(-serrN_2000,closestapproach(2:end)./1e3-2634,'b');
hold on
plot(B,closestapproach(2:end)./1e3-2634,'r');
hold on
plot(-B,closestapproach(2:end)./1e3-2634,'r');
shade(B,closestapproach(2:end)./1e3-2634,'r')
hold on
shade(serrN_2000,closestapproach(2:end)./1e3-2634,'b')
hold on
shade(-B,closestapproach(2:end)./1e3-2634,'r')
hold on
shade(-serrN_2000,closestapproach(2:end)./1e3-2634,'b')
title('Vertical electron density profile and uncertainty')
ylabel('Altitude [km]')
xlabel('Electron density [cm-3]')
xline(0)
legend('Average profile','1*\sigma','3*\sigma')

回答(1 个)

DGM
DGM 2021-11-18
I don't know what shade() you're using, since it's not part of MATLAB. Otherwise, you can do something with patch objects:
y = linspace(0,1,100);
x1 = (1+0.1*randn(1,100)).*(1-y).^0.25;
x2 = 2*(1+0.1*randn(1,100)).*(1-y).^0.25;
hold on
patch([x2 fliplr(-x2)],[y fliplr(y)],'r','facealpha',0.3,'edgecolor','r');
patch([x1 fliplr(-x1)],[y fliplr(y)],'b','facealpha',0.3,'edgecolor','b');
xlim([-2.5 2.5])

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by