Why doesn't the patch command fill homogeneously the region between two arrays?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Federico Geser
am 1 Jul. 2022
Bearbeitet: Federico Geser
am 1 Jul. 2022
I've done a fitting with lscov, calculated the confidence intervals and tried to plot the resulting line and use the patch command to paint the region limiting the confidence intervals of the fitting. Translating, I have three arrays, one is the fitting, and I want to color the region between the other two, like shown in the image (A). What I obtain is the image (B), not only in the figure environment, but also in the generated pdf.
The image looks better if I define a thinner axis with around 6000 points, but then the pdf takes ages to be generated (it weights >100MB) and sometimes the figure environment doesn't even load.
What could be happening here?
Example code used to generate (B):
x = 1:100:6000 ;
A = [1000 1500 3000 5000].' ;
B = [5 7 12 17].' ;
C = [2 3 2 1].' ;
[f,s] = lscov(A,B,1./C.^2) ;
figure,errorbar(A,B,C), hold on,
plot(x,x*f,'-r','LineWidth',1), hold on,
patch([x; flipud(x)], [x*(f-s); flipud(x*(f+s))],[1.0 0.0 0.0], ...
'EdgeColor',[1.0 0.0 0.0],'facealpha',0.3,'edgealpha',0.05), hold on,
xlim([0 6000]),
ylim([0 20]),
2 Kommentare
Rik
am 1 Jul. 2022
The image your code produces does not look like the B image you posted. It is not clear to me how you would get that with this code.
Akzeptierte Antwort
Cris LaPierre
am 1 Jul. 2022
Bearbeitet: Cris LaPierre
am 1 Jul. 2022
The issue I see is with how you are defining your patch inputs. Your x is a row vector, so the semicolon adds the flipped values to a second row. Remove the semicolons and use fliplr instead.
Where the line is linear, you really only need 4 points. That will greatly reduce the number of points in the figure, and speed up the exporting to pdf.
x = [1 6000] ; % reduced to just 2 points since the data is a line
A = [1000 1500 3000 5000].' ;
B = [5 7 12 17].' ;
C = [2 3 2 1].' ;
[f,s] = lscov(A,B,1./C.^2) ;
figure
errorbar(A,B,C)
hold on,
plot(x,x*f,'-r','LineWidth',1)
patch([x fliplr(x)], [x*(f-s) fliplr(x*(f+s))],[1.0 0.0 0.0], ...
'EdgeColor',[1.0 0.0 0.0],'facealpha',0.3,'edgealpha',0.05)
hold off
xlim([0 6000])
ylim([0 20])
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Polygons finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!