Clear Filters
Clear Filters

Shade area under a plot with MATLAB

64 views (last 30 days)
Gianmarco Broilo
Gianmarco Broilo on 17 Nov 2021
Answered: DGM on 18 Nov 2021
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')

Answers (1)

DGM
DGM on 18 Nov 2021
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])

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!