How do I fill the area between two plots in a loglog plot?

3 visualizaciones (últimos 30 días)
I am trying to fill in the area between the confidence interval of my pwelch. This is the code I am using:
[px1, f, conf1] = pwelch(det_total_b1,hanning(nfft),round(0.5*nfft),nfft,fs,'ConfidenceLevel',0.95);
[px2, f, conf2] = pwelch(det_total_b2,hanning(nfft),round(0.5*nfft),nfft,fs,'ConfidenceLevel',0.95);
[px3, f, conf3] = pwelch(det_total_b3,hanning(nfft),round(0.5*nfft),nfft,fs,'ConfidenceLevel',0.95);
[px4, f, conf4] = pwelch(det_total_b4,hanning(nfft),round(0.5*nfft),nfft,fs,'ConfidenceLevel',0.95);
loglog (f,px1,'Color',[0 204/256 102/256])
hold on
loglog (f,px2,'Color',[1 102/256 102/256])
loglog (f,px3,'Color',[0 128/256 1])
loglog (f,px4,'Color',[1 153/256 51/256])
patch([f fliplr(f)],[conf1(:,1) fliplr(conf1(:,2))],[0.85 0.85 0.85])
set(gca, 'XScale', 'log', 'YScale','log')
But instead of filling the area in gray, what it does is plotting the confidence interval limits as black lines (figure attached). I can't understand why it is doing that.
Thanks for the help in advance!
  2 comentarios
Star Strider
Star Strider el 31 de Oct. de 2019
I can’t run your code:
Unrecognized function or variable 'det_total_b1'.
Larissa Perez
Larissa Perez el 31 de Oct. de 2019
Hi, thanks for the quick feedback.
det_total_b1 is my data. It is just a detrended time-series. I guess you can run it by creating a random time-series, just to check if the patch works?

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 31 de Oct. de 2019
You have one fundamental error. Your matrices are (Nx2), so using fliplr on each column is pointless. You need to use flipud and vertically concatenate them:
patch([f; flipud(f)],[conf1(:,1); flipud(conf1(:,2))],[0.85 0.85 0.85]);
Perhaps the problem is with your data or what you are plotting, although when I simulated it with random (rand) column vectors, the data were real and greater than zero.
Using patch with loglog plots works in other contexts, for example:
x = sort(rand(10, 1));
y = sort(rand(10, 1));
figure
patch([x; flipud(x)], [y; flipud(y+0.2)], 'r')
set(gca, 'XScale','log', 'YScale','log')
  2 comentarios
Larissa Perez
Larissa Perez el 4 de Nov. de 2019
Great, thanks!
It was something silly, I had 0 as part of f. That's why..
Star Strider
Star Strider el 4 de Nov. de 2019
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by