[英]how to plot line of best fit using loglog in python matplotlib
Using this code:使用此代码:
x = np.array([1, 2, 7, 5, 8])
y = np.array([ 5, 4, 6, 7, 10 ])
x = np.log(x)
y = np.log(y)
m, b = np.polyfit(x, y, 1)
plt.plot(x, y, 'o')
plt.plot(x, m*x + b)
I can make a plot of log value as follow:我可以使日志值的 plot 如下:
But I want to have the axis ticks in non log-value, so I thought this would work:但我想让轴刻度为非对数值,所以我认为这会起作用:
x = np.array([1, 2, 7, 5, 8])
y = np.array([ 5, 4, 6, 7, 10 ])
m, b = np.polyfit(x, y, 1)
plt.loglog()
plt.plot(x, y, 'o')
plt.plot(x, m*x + b)
But I got this instead:但我得到了这个:
How do I make a best fit line in log scale but with non log axis ticks?如何在对数刻度中制作最佳拟合线,但使用非对数轴刻度?
If I understand correctly, you can set the xticklabels
/ yticklabels
to the exponential of the xticks
/ yticks
:如果我理解正确,您可以将
xticks
/ yticks
设置为xticklabels
/ yticklabels
的指数:
x = np.log(x)
y = np.log(y)
m, b = np.polyfit(x, y, 1)
fig, ax = plt.subplots()
ax.plot(x, y, 'o')
ax.plot(x, m*x + b)
ax.set_xticklabels([f'{tick:.1f}' for tick in np.exp(ax.get_xticks())])
ax.set_yticklabels([f'{tick:.1f}' for tick in np.exp(ax.get_yticks())])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.