matplotlib 中的多项式函数显示多行
Polynomial function in matplotlib displaying multiple lines
我遇到了这个问题中提出的相同问题:
该问题的解决方案似乎是根据 x 轴对点进行排序。就我而言,我很确定我的数据已经排序,因为我将 x 数组放入图中,如下所示:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.array([0,0,0,0,0,0,26,0,0,0,0,0,0,0,0,0,214,67,225,250,0,0,0,94,0,0,1366,137])
y = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,0,0,0,0,0,0,4,0])
fig3, ax3 = plt.subplots()
ax3.scatter(x, y)
ax3.plot(x, 0 + 0.004*x + 1.63e-06*(x**2), label='squared')
ax3.legend()
plt.show()
我只想绘制二次线:
您的示例确实在 x 轴上有未排序的数据,解决方案与您链接的问题相同:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.array([0,0,0,0,0,0,26,0,0,0,0,0,0,0,0,0,214,67,225,250,0,0,0,94,0,0,1366,137])
y = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,0,0,0,0,0,0,4,0])
order = np.argsort(x)
fig3, ax3 = plt.subplots()
ax3.scatter(x[order], y[order])
ax3.plot(x[order], 0 + 0.004*x[order] + 1.63e-06*(x[order]**2), label='squared')
ax3.legend()
plt.show()
我遇到了这个问题中提出的相同问题:
该问题的解决方案似乎是根据 x 轴对点进行排序。就我而言,我很确定我的数据已经排序,因为我将 x 数组放入图中,如下所示:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.array([0,0,0,0,0,0,26,0,0,0,0,0,0,0,0,0,214,67,225,250,0,0,0,94,0,0,1366,137])
y = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,0,0,0,0,0,0,4,0])
fig3, ax3 = plt.subplots()
ax3.scatter(x, y)
ax3.plot(x, 0 + 0.004*x + 1.63e-06*(x**2), label='squared')
ax3.legend()
plt.show()
我只想绘制二次线:
您的示例确实在 x 轴上有未排序的数据,解决方案与您链接的问题相同:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.array([0,0,0,0,0,0,26,0,0,0,0,0,0,0,0,0,214,67,225,250,0,0,0,94,0,0,1366,137])
y = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,0,0,0,0,0,0,4,0])
order = np.argsort(x)
fig3, ax3 = plt.subplots()
ax3.scatter(x[order], y[order])
ax3.plot(x[order], 0 + 0.004*x[order] + 1.63e-06*(x[order]**2), label='squared')
ax3.legend()
plt.show()