Problem with fill / patch

41 views (last 30 days)
Catriona Fyffe
Catriona Fyffe on 6 Mar 2021
Commented: Star Strider on 7 Mar 2021
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!

Accepted Answer

Star Strider
Star Strider on 6 Mar 2021
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 Comments
Catriona Fyffe
Catriona Fyffe on 7 Mar 2021
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 on 7 Mar 2021
As always, my pleasure!
I am happy that I was able to help you solve your problem, even if indirectly!

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!