我有以下幾點清單:
[[1,10],[2,5.49],[3,0.89],[4,-0.14],[5,-1.07],[6,0.84]]
我計算了通過這些點的拋物線y=0.83535714x^2-7.74778571x 17.116。價值:
> print(matrix)
[[ 0.83535714]
[-7.74778571]
[17.116 ]]
另外,我已經分開了幾點:
points = np.asarray([[1,10],[2,5.49],[3,0.89],[4,-0.14],[5,-1.07],[6,0.84]])
points_x_axis = points[:, 0]
points_y_axis = points[:, 1]
現在我想繪制拋物線和點。我該怎么做?
uj5u.com熱心網友回復:
使用多項式回歸平滑圖形
import numpy as np
import matplotlib.pyplot as plt
def parabola(x):
return 0.83535714*x**2-7.74778571*x 17.116
x = np.linspace(0, 9, 100)
y = parabola(x)
plt.plot(x, y)
plt.scatter(points_x_axis, points_y_axis)
plt.show()

#Simple plotting
import matplotlib.pyplot as plt
plt.plot(points_x_axis, points_y_axis, 'o')
plt.plot(points_x_axis, 0.83535714*points_x_axis**2-7.74778571*points_x_axis 17.116)
plt.show()
輸出:

uj5u.com熱心網友回復:
使用np.polyfit和np.polyval:
data = np.array([[1,10],[2,5.49],[3,0.89],[4,-0.14],[5,-1.07],[6,0.84]])
p = np.polyfit(data[:, 0], data[:, 1], 2)
x = np.linspace(0, 9, 100)
y = np.polyval(p, x)
plt.plot(x, y)
plt.plot(data[:, 0], data[:, 1], 'k.')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/357823.html
標籤:Python 麻木的 matplotlib
