필터 지우기
필터 지우기

Problem with fill / patch

조회 수: 26 (최근 30일)
Catriona Fyffe
Catriona Fyffe 2021년 3월 6일
댓글: Star Strider 2021년 3월 7일
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!

채택된 답변

Star Strider
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
Catriona Fyffe
Catriona Fyffe 2021년 3월 7일
Hi Star Strider,
Thanks for your time in trying to solve this for me! Using your code didn't initially solve the problem, in that I was getting some very odd graphs which were filled in a big wierd black square. But then comparing your data with what I had I realised that it was a dimension problem and just by transposing my data it then all worked, happy days :) This is the code that worked in the end.
x = datenum(SG_out.DateTime)';
y1 = MC_all_light.SG_MC_light.SN_ICE_D_stats.min';
y2 = MC_all_light.SG_MC_light.SN_ICE_D_stats.max';
figure();
plot(x, y1)
hold on
plot(x, y2)
fill([x fliplr(x)], [y1 fliplr(y2)], 'b')
hold off
Star Strider
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 CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by