How to fill plot between two lines

조회 수: 14 (최근 30일)
Fabian Moreno
Fabian Moreno 2021년 4월 14일
댓글: Star Strider 2021년 4월 14일
Hi there, I would like to know how to fill the area with one color (blues) if the area isabove the line and another color (red) if the area is under the line between two lines. I already make a code, but I couldn't fill the area under the line. I add my matrix in (.mat). I´m working with values in column five and six.Also I add my code and the image of what I get so far. Thank in advanced.
max=plot(data(:,1),data(:,5),'-k','LineWidth',3);grid on
xlim([data(1,1) data(365,1)]);
xticks([data(1) data(32) data(60) data(91) data(121) data(152) data(182) data(213) data(244) data(274) data(305) data(335)])
xticklabels({'Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez','Jan,20','Fev'})
ylabel('Accumulated precipitation [mm]')
xlabel('Month [daily data]')
hold on
min=plot(data(:,1),data(:,6),'-r');grid on
patch([max.XData, fliplr(min.XData)], [max.YData, fliplr(min.YData)], 'b')
hold off

채택된 답변

Star Strider
Star Strider 2021년 4월 14일
If I understand the objective correctly, this should work:
DL = load('data1.mat');
data = DL.data;
max=plot(data(:,1),data(:,5),'-k','LineWidth',3);grid on
xlim([data(1,1) data(365,1)]);
xticks([data(1) data(32) data(60) data(91) data(121) data(152) data(182) data(213) data(244) data(274) data(305) data(335)])
xticklabels({'Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez','Jan,20','Fev'})
ylabel('Accumulated precipitation [mm]')
xlabel('Month [daily data]')
hold on
min=plot(data(:,1),data(:,6),'-r');
above = min.YData > max.YData; % Logical Vector
patch([max.XData(above), fliplr(min.XData(above))], [max.YData(above), fliplr(min.YData(above))], 'b') % Blue Area
patch([max.XData(~above), fliplr(min.XData(~above))], [max.YData(~above), fliplr(min.YData(~above))], 'r') % Red Area
hold off
grid on
It adds a logical vector (‘above’), then uses that to colour the areas appropriately. The rest of the code is unchanged.
  댓글 수: 9
Fabian Moreno
Fabian Moreno 2021년 4월 14일
편집: Fabian Moreno 2021년 4월 14일
Star Strider, I found why it didn´t fill red just the two areas under the line. It was because the two areas under the line become a kind of polygone and not recognized the two areas. Now, I divided the two areas and fill separated each one with red. Any way, thank you, now I understand a little bit more. :). I really aprecciated your help.
max=plot(data(:,1),data(:,5),'-k','LineWidth',3);grid on
xlim([data(1,1) data(365,1)]);
xticks([data(1) data(32) data(60) data(91) data(121) data(152) data(182) data(213) data(244) data(274) data(305) data(335)])
xticklabels({'Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez','Jan,20','Fev'})
ylabel('Precipitação acumulada [mm]')
xlabel('Meses [datos diários]')
hold on
min=plot(data(:,1),data(:,6),'-r');
above = find(data(:,5) > data(:,6)); % Logical Vector
above1= above(1:3,1); % here I divided the two areas
above2= above(4:end,1);
patch([max.XData, fliplr(min.XData)], [max.YData, fliplr(min.YData)], 'b') % Blue Area
patch([max.XData(above1), fliplr(min.XData(above1))], [max.YData(above1), fliplr(min.YData(above1))], 'r') % Red Area
patch([max.XData(above2), fliplr(min.XData(above2))], [max.YData(above2), fliplr(min.YData(above2))], 'r')
Star Strider
Star Strider 2021년 4월 14일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by