How to fill plot between two lines
显示 更早的评论
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

采纳的回答
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 个评论
I don't know why, but some areas did't get the colour propertly.

Those are the only areas visible on the plot that are below the black line.
The code works correctly if I understand your variables correctly. There are only 9 values (out of 365) that are not above the black line.
Check the ‘above’ calculation. If it is in error, it can likely be easily corrected.
Note that the code is not at all documnented, and I have no idea what the reference values are (I guessed that they are the black line), so I had to guess as to what was wanted.
Given the information provided, my code works correctly.
Thank you Star Strider, definetly the code works correctly. Just I don't understand the calculation 'above'. That's why I ask again. I already try differents calculations and I couldn't.
You are right abour the reference line (is tha black line).
My pleasure!
The ‘above’ calculation creates a logical vector such that 1 or true is above the black line and 0 or false is below the black line. Here, I changed the first patch call to use the true values to plot those areas above the black line in blue, and a second added patch call to use the false values (created by the ~ negation operator to convert them to true for that operation, and the previous true values to false so that exactly the reverse condition would apply) to plot those areas below the black line in red.
I realise that this may be confusing, however I cannot describe it any other way and remain faithful to how the code actually works. Essentially, true values plot and false values do not plot.
Logical indexing is a very efficient way way of indexing arrays. See the documentation section on Matrix Indexing for details.
If my Answer helped you solve your problem, please Accept it!
.
Star Strider, one more question. I already read, but I didn´t understand what is the function of 'fliplr'. I saw that in a matrix it change the order of vectors. But what does it in the graph?
That has to do with the way patch works. The patch function fills a closed area, so in order to create the closed area, it is necessary to create it by first going in one direction in the x-vector, then going in one direction in the y-vector, then reversing those directions to close the area. Reversing the directions is created by using the various ‘flip’ commands. (A side note is that horizontal concatenation of a row vector with a fliplr version of it needs to be changed to vertical concatenation of a column vector with its flipud version.)
This is a very brief description. I encourage you to experiment with that idea on your own to fully understand how patch works.
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')

As always, my pleasure!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
