problem with polyval plotting

5 Ansichten (letzte 30 Tage)
Teshan Rezel
Teshan Rezel am 13 Jul. 2021
Beantwortet: dpb am 14 Jul. 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 Kommentare
dpb
dpb am 13 Jul. 2021
Bearbeitet: dpb am 13 Jul. 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 am 13 Jul. 2021
Bearbeitet: Teshan Rezel am 13 Jul. 2021
@dpb thank you! can you please post this as an answer so I can accept?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 14 Jul. 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);

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by