problem with polyval plotting

3 views (last 30 days)
Teshan Rezel
Teshan Rezel on 13 Jul 2021
Answered: dpb on 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 Comments
dpb
dpb on 13 Jul 2021
Edited: dpb on 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 on 13 Jul 2021
Edited: Teshan Rezel on 13 Jul 2021
@dpb thank you! can you please post this as an answer so I can accept?

Sign in to comment.

Accepted Answer

dpb
dpb on 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);

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!