How can I fill area above a plot?

Hello, I am trying to fill the area above a curve in my top subplot. I have already filled the area beneath my lower curve but cannot seem to get the area above the upper curve. Both curves are create from sets of data points.
ax1 = subplot(2,1,1);
plot(Freq1,Gain_Upper)
hold on
basevalue = -15;
ar=area(Freq2,Gain_Lower,basevalue);
Hz vs dB.PNG

 Accepted Answer

Try this:
x = 0:10:90; % Create Data
G1 = [-1 -2 -3 -4 -5 -7 -9 -11 -12 -13]; % Create Data
G2 = [1 1 2 2 2 2 2 2 2 2]; % Create Data
figure
plot(x, G2)
axis([0 100 -15 5])
hold on
plot(x, G1)
patch([x fliplr(x)], [G1 min(ylim)*ones(size(G1))], 'r') % Below Lower Curve
patch([x fliplr(x)], [G2 max(ylim)*ones(size(G2))], 'r') % Above Upper Curve
hold off
grid
How can I fill area above a plot - 2019 05 15.png
Experiment to get the result you want.

4 Comments

I was able to re-create your answer with my data but when I add more data and seperate them into subplots the fill gets confused. Do I need to change the patch limits?
figure(4)
% top subplot
ax1 = subplot(2,1,1);
% Gain Plot Values
Freq1 = [1;10;20;100]; % x2
Gain_Upper = [1;1;2;2]; % G2
Freq2 = [1;50;100]; % x1
Gain_Lower = [-1;-3;-12]; % G1
plot(Freq1,Gain_Upper)
axis([0 100 -15 5])
hold on
plot(Freq2,Gain_Lower)
patch([Freq1 fliplr(Freq1)], [Gain_Upper max(ylim)*ones(size(Gain_Upper))], 'b') % Above Upper Curve
patch([Freq2 fliplr(Freq2)], [Gain_Lower min(ylim)*ones(size(Gain_Lower))], 'r') % Below Lower Curve
hold off
grid
title('Frequency Response Requirement')
xlabel('Frequency [Hz]')
ylabel('Gain [dB]')
% bottom subplot
ax2 = subplot(2,1,2);
% Phase Plot Values
Freq3 = [1;10;40;70;100];
Phase = [-5;-20;-70;-135;-180];
basevalue3 = -200;
area(Freq3,Phase,basevalue3)
axis([0 105 -200 0])
grid on
xlabel('Frequency [Hz]')
ylabel('Phase(deg)')
Hz vs dB.PNG
The problem is simply that my original code uses row vectors, and your data are column vectors. The MATLAB plotting functions treat them differently.
The easiest way to fix that is to transpose your vectors in the patch calls:
patch([Freq1' fliplr(Freq1')], [Gain_Upper' max(ylim)*ones(size(Gain_Upper'))], 'b') % Above Upper Curve
patch([Freq2' fliplr(Freq2')], [Gain_Lower' min(ylim)*ones(size(Gain_Lower'))], 'r') % Below Lower Curve
producing this plot:
Note the use of the transpose operator ('). (The transpose operator has two forms, the complex-conjugate transpose (') and the simple transpose (.'). Your data are real, so there is no difference. I use (') here for convenience.)
Oh okay, I got it now. Thank you!!
As always, my pleasure!

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 15 May 2019
Edited: Adam Danz on 15 May 2019
Here you go (applied to fake data); let me know if you have any questions.
% Create fake data
Freq1 = 1:100;
Gain_Upper = linspace(0,-200,numel(Freq1))+rand(size(Freq1))*10;
% plot
figure
ax1 = axes;
plot(ax1,Freq1,Gain_Upper, 'k-','LineWidth',3)
% Form polygon
ceiling = max(ylim(ax1)); %define top of polygon as top of y axis
xp = [Freq1,fliplr(Freq1)];
yp = [Gain_Upper, repmat(ceiling,size(Freq1))];
hold(ax1, 'on')
% Fill area above curve
fill(ax1,xp,yp,'g')
If you'd like to fill the area between two lines use this line instead:
yp = [Gain_Upper, UpperLineValues];
% Where UpperLineValues contain the y coordinates of the upper line
190515 111012-Figure 1.jpg

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!