problem with polyval plotting

4 次查看(过去 30 天)
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 个评论
dpb
dpb 2021-7-13
编辑:dpb 2021-7-13
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 2021-7-13
编辑:Teshan Rezel 2021-7-13
@dpb thank you! can you please post this as an answer so I can accept?

请先登录,再进行评论。

采纳的回答

dpb
dpb 2021-7-14
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by