我正在使用Matplotlib绘制一些数据,但是自动轴标签已关闭。 X轴在标签中应为负值,而负数应为正,而Y轴的比例非常奇怪。
以下是一些代码:
import numpy as np
import numpy.polynomial.polynomial as poly
import matplotlib.pylab as plot
# pass in same sized arrays x, y, and z, returns fit of x to z and y to z
def fitData(x, y, z):
x_coefs = poly.polyfit(x, z, 1)
xfit = poly.Polynomial(x_coefs) # instead of np.poly1d
y_coefs = poly.polyfit(y, z, 3)
yfit = poly.Polynomial(y_coefs) # instead of np.poly1d
return xfit, yfit
x = [3.08, 3.1 , 3.12, 3.14, 3.16, 3.18, 3.2 , 3.22, 3.24,
3.26, 3.28, 3.3 , 3.32, 3.34, 3.36, 3.38, 3.4 , 3.42,
3.44, 3.46, 3.48, 3.5 , 3.52, 3.54, 3.56, 3.58, 3.6 ,
3.62, 3.64, 3.66, 3.68]
y = [0.000857, 0.001182, 0.001619, 0.002113, 0.002702, 0.003351,
0.004062, 0.004754, 0.00546 , 0.006183, 0.006816, 0.007362,
0.007844, 0.008207, 0.008474, 0.008541, 0.008539, 0.008445,
0.008251, 0.007974, 0.007608, 0.007193, 0.006752, 0.006269,
0.005799, 0.005302, 0.004822, 0.004339, 0.00391 , 0.003481,
0.003095]
z = np.linspace(0, 1, num=len(x)) # create as many z points as input data
xfit, yfit = fitData(x, y, z)
ax1 = plot.subplot(1, 2, 1)
plot.title("X position")
plot.xlabel("Z (ft)")
plot.ylabel("X (ft)")
ax1.plot(z, xfit(z))
ax2 = plot.subplot(1, 2, 2)
plot.title("Y position")
plot.xlabel("Z (ft)")
plot.ylabel("Y (ft)")
ax2.plot(z, yfit(z))
plot.tight_layout()
plot.show()
这是我的合成图:
如您所见,X绘图比例条上的范围似乎反向,而y绘图比例远。有谁知道为什么会这样?
答案 0 :(得分:1)
通常,我假设Matplotlib知道您的数据范围,因此我将从查看其绘制的数据开始。查看x数据,y截距应为3.08,而x_coefs为array([-5.13333333, 1.66666667]
,因此您的fit函数会关闭(ipython或print(x_coefs)是您的朋友)。
您需要颠倒x_coefs = poly.polyfit(x, z, 1)
中的y_coefs
中的x和z。