Problem with fill / patch
조회 수: 26 (최근 30일)
이전 댓글 표시
Hi, I would like to use fill or patch to colour the space between two lines. I have a minimum value and a maximum value and both are plotted against a datetime vector. I have tried to follow carefully other instructions for using patch or fill but I am not getting what I expect - any ideas where I am going wrong? Many thanks!
%With fill and flip lr
x=[1:1:14568]'; %#initialize x array
y1=MC_all_light.SG_MC_light.SN_ICE_D_stats.min; %#create first curve
y2=MC_all_light.SG_MC_light.SN_ICE_D_stats.max; %#create second curve
X=[x,fliplr(x)]; %#create continuous x value array for plotting
Y=[y1,fliplr(y2)]; %#create y values for out and then back
figure();fill(X,Y,'b');
%Basically the same with patch and flipud. Here I am using DateTime, but I get the same results with a vector as for fill.
figure();
patch([datenum(SG_out.DateTime) flipud(datenum(SG_out.DateTime))], [MC_all_light.SG_MC_light.SN_ICE_D_stats.max flipud(MC_all_light.SG_MC_light.SN_ICE_D_stats.min)], [0.6 0.8 1.0])
The first graph is with fill the second with patch. I really want the colour to be between the max and min lines!
댓글 수: 0
채택된 답변
Star Strider
2021년 3월 6일
It would help to have your data.
Try something like this:
x = linspace(0, 15000, 1000);
y1 = (500*rand(size(x))-1E-5*(7000-x).^2)*1E-2 + 54.5-x*1E-3;
y2 = (500*rand(size(x))-1E-5*(7000-x).^2)*1E-2 + 54.5-2*x*1E-3;
y1y2 = [y1; y2];
figure
plot(x, y1)
hold on
plot(x, y2)
fill([x fliplr(x)], [min(y1y2) fliplr(max(y1y2))], 'b')
hold off
If you want to colour between ther max and min values (and since it appears that both curves have the same number of points), tell fill to do just that! (If you are using datetime arrays, fill is preferable to patch, since patch will not work with datetime arrays.)
댓글 수: 2
Star Strider
2021년 3월 7일
As always, my pleasure!
I am happy that I was able to help you solve your problem, even if indirectly!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!