Why doesn't the patch command fill homogeneously the region between two arrays?

2 Ansichten (letzte 30 Tage)
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
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.
Federico Geser
Federico Geser am 1 Jul. 2022
Thanks @Rik for your answer. The code I attached is exactly the one I'm using (there is nothing else in my code except what I showed here). But in my computer it generates the image (B) I shared (which is a crop of the zommed image). Maybe you can see it clearly using x = 1:100:6000 instead of 1:1:6000.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Cris LaPierre
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])

Weitere Antworten (0)

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by