Shade between time series lines (log-y scale)

Hello,
I'm attempting to plot between two lines on a graph that's set to a log-y scale. I've tried almost every iteration of the "fill" command that seems to appear from various sources online, which usually gives me somewhat psychedelic (but useless) results.
Suppose that:
x = 0:100
y1 = rand(1,100)+1.5
y2 = rand(1,100)+0.5
How can I fill just between the y1 and y2 lines??? I want nothing but empty space below the y2 line. Also, this is on a log-y scale, which is why I can't just tweak the "area" commands (like I did for a similar project that was not on a log-y scale.
Please help!!!
Thanks, Ryan

 Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de En. de 2012
Your x starts from 0 (and is one point longer than y1 or y2.) If you set the axis XScale to log, then you are attempting to take log(0), which leads to problems in drawing the patch.
Without the 0 I had no problem.
x = 1:100;
y1 = rand(1,100)+1.5;
y2 = rand(1,100)+0.5;
fill([x fliplr(x)],[y1 fliplr(y2)],'r')
pause(3)
set(gca,'XScale','log','YScale','log')
drawnow()

4 comentarios

Mireia Fontanet
Mireia Fontanet el 3 de Nov. de 2018
Editada: Walter Roberson el 3 de Nov. de 2018
Dear,
I am trying to fill an area between two lines and reproduce the same example that you showed. I don't know why but I can not fillthe area. My code is:
A=xlsread('TAW_NZ');
x=A(:,1);
min1=A(:,6);
max1=A(:,7);
figure
plot(x,min1,x,max1);
fill([x,fliplr(x)],[min1,fliplr(max1)],'r')
Thanks
Your x data has 100 valid data points, followed by 100 nan. There are also some issues because your values are column oriented
x = A(1:100,1);
min1 = A(1:100,6);
max1 = A(1:100,7);
figure
plot(x, min1, x, max1);
fill([x.',fliplr(x.')], [min1.',fliplr(max1.')], 'r')
Mireia Fontanet
Mireia Fontanet el 4 de Nov. de 2018
I have this result, why?
You did not use my revised code that has the x.' and so on.
To give it again in full:
A = xlsread('TAW_NZ');
x = A(1:100,1);
min1 = A(1:100,6);
max1 = A(1:100,7);
figure
plot(x, min1, x, max1);
fill([x.',fliplr(x.')], [min1.',fliplr(max1.')], 'r')

Iniciar sesión para comentar.

Más respuestas (3)

Venn Ravichandran
Venn Ravichandran el 19 de En. de 2012

0 votos

Using stem plot might be a quick work around. I don't know why the area plots don't work.
the cyclist
the cyclist el 19 de En. de 2012
Will this suffice?
x = 1:100;
y1 = rand(1,100)+1.5;
y2 = rand(1,100)+0.5;
figure
plot(x,y1,x,y2)
fill([x,fliplr(x)],[y1,fliplr(y2)],'r')
set(gca,'YScale','log')
[Edited to include the additional fliplr, in response to Walter's comment.]

1 comentario

Walter Roberson
Walter Roberson el 19 de En. de 2012
I think you have to flip y2 as well.
Amusing that we choose the same color and same basic technique :)

Iniciar sesión para comentar.

Ryan Utz
Ryan Utz el 19 de En. de 2012

0 votos

Thanks Walter and the cyclist! This does work on my more complex data. The command "fliplr" is new to me, and useful...

Etiquetas

Preguntada:

el 19 de En. de 2012

Comentada:

el 4 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by