problem with polyval plotting

4 ビュー (過去 30 日間)
Teshan Rezel
Teshan Rezel 2021 年 7 月 13 日
回答済み: dpb 2021 年 7 月 14 日
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 Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by