Fill between two lines not working
Mostrar comentarios más antiguos
I am trying to fill the between the lines but its is only plotting the limiting lines. I cant seem to understand the problem. Hope you can help. Thanks in advance.
figure(2)
semilogx((f_Along)*(0.1/WS),f_Along.*((PSD_AL_M))/WSP^2,'Color',...
'k','LineWidth',2)
hold on
semilogx((f_Along)*(0.1/WS),f_Along.*((SD_AL_P))/WSP^2,'Color',...
[0.7 0.7 0.7],'LineWidth',2,'DisplayName','STD+')
hold on
semilogx((f_Along)*(0.1/WS),f_Along.*((SD_AL_N))/WSP^2,'Color',...
[0.7 0.7 0.7],'LineWidth',2,'DisplayName','STD-')
hold on
x2 = [(f_Along')*(0.1/WS), fliplr((f_Along')*(0.1/WS))];
inBetween = [f_Along'.*((SD_AL_P'))/WSP^2, fliplr(f_Along'.*((SD_AL_N'))/WSP^2)]
fill(x2, inBetween, 'k');
hold on
3 comentarios
Walter Roberson
el 9 de Abr. de 2020
We will probably need your variables to test with.
Walter Roberson
el 10 de Abr. de 2020
It would be easier if we had your data so we could test on our own system to avoid accidentally giving you a solution that does not work for your situation.
Saugat Shrestha
el 10 de Abr. de 2020
Respuestas (1)
Star Strider
el 9 de Abr. de 2020
0 votos
Plot everything (including the patch calls) using linear axis rulers first, then convert the scale to logarithmic.
.
8 comentarios
Saugat Shrestha
el 10 de Abr. de 2020
Star Strider
el 10 de Abr. de 2020
It is necessary to plot in the linear scale, including the patch calls, then do:
set(gca, 'XScale','log')
Example —
x = logspace(-2, 1, 250);
y = sin(2*pi*x);
hi = y + 0.2;
lo = y - 0.2;
figure
plot(x, y)
hold on
patch([x, fliplr(x)], [hi fliplr(lo)], 'r', 'FaceAlpha',0.25)
hold off
grid
set(gca, 'XScale','log')
Run this example, then apply it to your code.
Saugat Shrestha
el 10 de Abr. de 2020
Walter Roberson
el 10 de Abr. de 2020
what is min() of the y coordinates of the data you are trying to fill between?
Saugat Shrestha
el 10 de Abr. de 2020
Walter Roberson
el 10 de Abr. de 2020
fill() creates a 2D patch with a continuous edge.
In MATLAB, when you use patch() on a log scale, then any face that has a coordinate of 0, or a negative number, or nan, in it is not drawn. The edges that are entirely within the limits would still be drawn, though. This leads to situations where the edge of a fill area is drawn but the area is not filled.
This is happening to you because your first and last x2 coordinates are 0. Based on your sample graphs you probably want to use max(1e-4,x2) as the x coordinates.
Star Strider
el 10 de Abr. de 2020
@Saugat Shrestha — It would have helped to have had your data at the outset. As Walter points out, the problem is that ‘f_Along’ begins with 0. Since log(0) appraches -Inf, the transformation fails for patch.
Add before you create ‘figure(2)’:
f_Along = f_Along + eps;
and after the patch (or fill) call:
xlim([f_Along(2) f_Along(end)])
set(gca, 'XScale','log')
@Walter — Thank you.
.
Saugat Shrestha
el 11 de Abr. de 2020
Categorías
Más información sobre Discrete Data Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
