problem with polyval plotting

4 vues (au cours des 30 derniers jours)
Teshan Rezel
Teshan Rezel le 13 Juil 2021
Réponse apportée : dpb le 14 Juil 2021
Hi folks,
I am trying to plot a trendline on a scattergraph but I'm not sure why the following is happening! Any help will be appreciated!
My initial code and result:
sumStart = 72;
exclude1 = cri > 30;
for i = 1 : numSamples
allCokePercentSum(i) = sum(allCokePercent(sumStart:end, i));
anisotropicPercentSum(i) = sum(anisotropicPercent(sumStart:end, i));
isotropicPercentSum(i) = sum(isotropicPercent(sumStart:end, i));
fillerPercentSum(i) = sum(fillerPercent(sumStart:end, i));
end
x_labelString = ['Histogram Percentage Sum (Lower Threshold = ' num2str(sumStart) ')'];
[allCokeFit, GOF1] = polyfit(cri, allCokePercentSum', 3);
[anisotropicFit, GOF2] = polyfit(cri, anisotropicPercentSum', 3);
[isotropicFit, GOF3] = polyfit(cri, isotropicPercentSum', 3);
[fillerFit, GOF4] = polyfit(cri, fillerPercentSum', 3);
allCokeLine = polyval(allCokeFit, cri);
anisotropicLine = polyval(allCokeFit, cri);
isotropicLine = polyval(allCokeFit, cri);
fillerLine = polyval(allCokeFit, cri);
figure;
hold on
subplot(2, 2, 1);
plot(allCokeLine, allCokePercentSum, cri, exclude1);
xlabel(x_labelString, 'FontWeight',"bold");
ylabel('CRI', 'FontWeight',"bold");
title('Greyscale Histogram Percentage Counts for "All-Coke" Threshold')
subplot(2, 2, 2);
plot(anisotropicLine, anisotropicPercentSum, cri, exclude1);
xlabel(x_labelString, 'FontWeight',"bold");
ylabel('CRI', 'FontWeight',"bold");
title('Greyscale Histogram Percentage Counts for "Anisotropic" Threshold')
subplot(2, 2, 3);
plot(isotropicLine, isotropicPercentSum, cri, exclude1);
xlabel(x_labelString, 'FontWeight',"bold");
ylabel('CRI', 'FontWeight',"bold");
title('Greyscale Histogram Percentage Counts for "Isotropic Threshold')
subplot(2, 2, 4);
plot(fillerLine, fillerPercentSum, cri, exclude1);
xlabel(x_labelString, 'FontWeight',"bold");
ylabel('CRI', 'FontWeight',"bold");
title('Greyscale Histogram Percentage Counts for "Filler" Threshold')
hold off
  4 commentaires
dpb
dpb le 13 Juil 2021
Modifié(e) : dpb le 13 Juil 2021
The coefficient array from polyfit for polyval for each plot/variable...
ADDENDUM
You might find it more visually appealing to not use the line marker for the data on the plots, though...just plot the markers for data and add the regression line. plot() draws straight lines between all points in succession; that may be too "busy" or distracting...
Teshan Rezel
Teshan Rezel le 13 Juil 2021
Modifié(e) : Teshan Rezel le 13 Juil 2021
@dpb thank you! can you please post this as an answer so I can accept?

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 14 Juil 2021
Your x values aren't sorted so they're being plotted with connecting lines in the order they are in the input arrays ...
The code is pretty convoluted and uses a lot of variables, but the idea is
[x,ix]=sort(x); % sort the independent variable, save the sort index
y=y(ix); % sort y same order to match
yHat=polyval(b,x); % use coefficients, b, from polyfit to evaluate the fit in sorted order, too...
plot(x,y,x,yHat) % and plot
If don't want to overwrite the original variables, can make a temporary, of course..
ADDENDUM
You might find it more visually appealing to not use the line marker for the data on the plots, though...just plot the markers for data and add the regression line. plot() draws straight lines between all points in succession; that may be too "busy" or distracting...
ADDENDUM SECOND
BTW, it only takes two points to draw the regression line since it is linear...
xHat=[min(x) max(x)];
yHat=polyval(b,xHat);

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by