![How can I fill area above a plot - 2019 05 15.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/219688/How%20can%20I%20fill%20area%20above%20a%20plot%20-%202019%2005%2015.png)
How can I fill area above a plot?
108 次查看(过去 30 天)
显示 更早的评论
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/219685/image.png)
0 个评论
采纳的回答
Star Strider
2019-5-15
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/219688/How%20can%20I%20fill%20area%20above%20a%20plot%20-%202019%2005%2015.png)
Experiment to get the result you want.
4 个评论
更多回答(1 个)
Adam Danz
2019-5-15
编辑:Adam Danz
2019-5-15
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/219687/190515%20111012-Figure%201.jpeg)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!