在对数刻度上绘制趋势线 [英] Plotting a trendline on a logarithmic scale

查看:204
本文介绍了在对数刻度上绘制趋势线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将趋势线添加到semilogx图中,但无法成功.我想要在y(17)y(20)之间的趋势线,但是没有将其绘制为直线.

I am trying to add a trendline to a semilogx plot but cannot succeed. I want the trendline between y(17) and y(20), but it is not plotted as a straight line.

这是我的代码:

%// Define equation.
x = [90868 68151 45434 34076 27261 13631 6816 3408 2273 1948 1705 1137 853 683 569 455 342 274 228 190]; 
y = [3680 3723 3800 3866 3920 4103 4250 4320 4340 4344 4350 4364 4373 4379 4384 4393 4398 4402 4405 4407];

%// Plot it
semilogx(x,y, 'bo-', 'LineWidth', 3); 
grid on; 

%// Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

%// Give a name to the title bar. 
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

%// Fit the y data range with a line (limitedRange).
limitedRange = 17:20;
coeffs = polyfit(x(limitedRange), y(limitedRange), 1);
xFitting = linspace(200, 90000, 50);
yFitted = polyval(coeffs, xFitting);

%// Plot the fitted line over the specified range.
hold on;
plot(xFitting, yFitted, 'ro-', 'LineWidth', 2);
legend('Original Data', 'Line Fit');

如何使趋势线显示为一条线?

How do I make the trendline appear as a line?

推荐答案

在这里应该没有对polyfit的抱怨,它可以正常工作.

There should be no complaints for polyfit here, it works.

这是你的情节:

一切都说得通.线变形的原因是因为您的x轴具有对数刻度(如果在对数x刻度上绘制ax+b线,则会将其视为alogx+b曲线).

Everything makes sense. The reason your line gets distorted is because your x-axis has a logarithmic scale (if you're plotting a line ax+b on a logarithmic x-scale, you'll see it as a alogx+b curve).

要将其显示为对数x轴上的线,您将需要引入适当的反"失真.在您的情况下,趋势线应按以下方式计算:

To see it as a line on a logarithmic x-axis, you will need to introduce the appropriate "inverse" distortion. In your case, the trendline should be computed like so:

limitedRange = 17:20;
coeffs = polyfit(log10(x(limitedRange)), y(limitedRange), 1); %// Note the log10
xFitting = linspace(200, 90000, 50);
yFitted = polyval(coeffs, log10(xFitting));                   %// Note the log10

这还不是全部.在对数刻度中,较低的x坐标往往会更隔开,并且在x轴的较高值处趋势线圆会更密集.要消除这一点,您需要将xFitting中的点按线性比例指数间隔,以便它们以对数比例线性显示,例如:

This is not all. In a logarithmic scale low x-coordinates tend to be more spaced out, and the trendline circles will be more dense at the higher values of the x-axis. To negate that, you need the points in xFitting to be exponentially spaced on a linear scale, so that they would appear linearly spaced on the logarithmic scale, for instance:

xFitting = 10 .^ (1:.1:5);

或使用内置的 logspace 函数:

or use the built-in logspace function:

xFitting = logspace(1, 5, 50);

用于计算趋势线的最终代码应为:

The final code for computing the trendline should be:

limitedRange = 17:20;
coeffs = polyfit(log10(x(limitedRange)), y(limitedRange), 1);
xFitting = logspace(1, 5, 50);
yFitted = polyval(coeffs, log10(xFitting));

这应该给你下面的情节:

And this should give you the following plot:

再次,请记住,这是对数刻度!

Again, keep in mind that this is a logarithmic scale!

希望这会有所帮助:)

这篇关于在对数刻度上绘制趋势线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆