How to get the patch function to fill in between 2 lines ?

10 vues (au cours des 30 derniers jours)
Hazel Lee
Hazel Lee le 11 Nov 2020
I am trying ti shade the space between an upper and lower limit. Time is a categorical variable so I created 'x' as a numerical array of the same length. But when i plot, it splits the graph into polygons but doesn't fill them. Can someone help with where I'm going wrong?
plot(time,y1);
hold on
plot(time,y2);
x = (1:1:12)';
patch([x fliplr(x)].',[y1 fliplr(y2)].','b');
  1 commentaire
Ameer Hamza
Ameer Hamza le 11 Nov 2020
Without the variables used in this code, it is difficult to see the issue.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Nov 2020
%create some data to illustrate the code
time = categorical({'t01', 't02', 't03', 't04', 't05', 't06', 't07', 't08', 't09', 't10'});
x = 1:length(time);
y1 = x.^2;
y2 = x.^3 / 3 - 2*x.^2 + 5;
%now do the plotting
plot(time, y1, time, y2);
hold on
fill([time fliplr(time)].',[y1 fliplr(y2)].', 'b', 'edgecolor', 'none', 'facealpha', 0.1);
fill() uses patch with face colors set appropriately.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by