简体   繁体   English

Python3 / Matplotlib:尝试在对数-对数刻度上绘制直线会导致部分弯曲的线

[英]Python3/Matplotlib: attempt at drawing straight line on log-log scale results in partially curved line

I tried to plot a straight line (linear slope: k = -1.518581016210938 , linear constant (=offset) b = 6.300735205027388 [those numbers were calculated from my data to which I'm trying to plot a best-fit line; I tried a couple of the answers related to plotting logarithmic best-fit lines, but I couldn't get them to work either]) into a log-log scale using matplotlib, but the resulting line looks like it has a curve at the top. 我试图绘制一条直线(线性斜率: k = -1.518581016210938 ,线性常数(=偏移) b = 6.300735205027388 [这些数字是根据我想绘制最合适的线的数据计算得出的;几个与绘制对数最佳拟合线有关的答案,但是我也无法使它们起作用]),使用matplotlib将其绘制成对数对数刻度,但是所得的线看起来像是在顶部有一条曲线。 Here's my code: 这是我的代码:

import math

k = -1.518581016210938
b = 6.300735205027388

ext = 1000000

vaakaplot = range(0, 16308+ext)
pystyplot = [10**(k*(math.log(n+1, 10))+b) for n in vaakaplot]

from matplotlib import pyplot as plt

plt.xscale('log')
plt.yscale('log')

plt.plot(vaakaplot, pystyplot, 'b-')

plt.axis([1, 10**6, 1, 10**6])

plt.show()

The output is as follows: 输出如下:

在此处输入图片说明

Can anyone tell me what's wrong and how to fix this? 谁能告诉我哪里出了问题以及如何解决此问题?

There's nothing "wrong", per se: a straight line in one frame is not necessarily a straight line in another. 本质上没有“错误”:一帧中的直线不一定是另一帧中的直线。 This is one of those cases. 这是其中一种情况。

A straight line is equivalent to having a constant slope. 直线等效于具有恒定的斜率。 What you're plotting here, interpreted in a "normal" scale, is the points ( log(x), log(y) ) . 您在此处以“标准”比例解释的绘图是点( log(x), log(y) ) As long as the quantities are large, this appears to be a straight line on the log-log scale as well. 只要数量很大,这似乎也是对数-对数刻度上的一条直线。 The slope at any given point past about x=15 is close to k . 超过x = 15的任意给定点的斜率接近k

However, when x is closer to 0, the tangent involves that +b term in both numerator and denominator. 但是,当x接近0时,切线在分子和分母中都包含+b项。 That is what throws off the values you expected. 就是抛出您期望值的原因。 y = kx + b is linear, but your ratio log(y) / log(x) is not. y = kx + b是线性的,但是您的比率log(y) / log(x)不是。

Try printing the graph in the range 1-10, and you'll see the effect with better magnification. 尝试在1到10的范围内打印图形,您会看到放大效果更好的效果。

The relationship you defined is not linear in the log-log domain. 您定义的关系在log-log域中不是线性的。 For it to be a straight line, we need to be able to substitute x for log(x) and y for log(y) and end up with an equation that looks like y = m*x + b. 为了使其成为一条直线,我们需要能够用x代替log(x),用y代替log(y),最后得到一个看起来像y = m * x + b的方程。

In your definition of the y-values, you have added 1 to each value of x. 在y值的定义中,已将x的每个值加1。 The resulting relationship looks like: 产生的关系如下所示:

在此处输入图片说明

You cannot substitute log(y) -> y and log(x) -> x without first transforming the x variable. 如果不先转换x变量, 就不能替换log(y)-> y和log(x)-> x。 In other words, it is not a linear relationship of log(x) vs log(y), so you should not expect a straight line. 换句话说,它不是log(x)与log(y)的线性关系,因此您不应期望直线。

The reason it only looks bent near the left side of the graph is because the effect +1 gets washed out by n as n gets large. 它看起来仅在图形左侧弯曲的原因是,随着n变大,效果+1n冲走。

To get a straight line, you can change math.log(n+1, 10) to math.log(n, 10) 要获得一条直线,可以将math.log(n+1, 10) math.log(n, 10) math.log(n+1, 10)更改为math.log(n, 10)

vaakaplot = range(1, 16308+ext)
pystyplot = [10**(k*(math.log(n, 10))+b) for n in vaakaplot]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM