不能将序列乘以非整数

Can't multiply sequence by non-int

以下代码是我不确定如何解决的错误的最小示例。

import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

a, b, c = np.polyfit(x, y, 2)
fig, ax = plt.subplots()
ax.plot(x, a*x)

我做错了什么?

您试图将列表与 non-integer 相乘。相反,使用 numpy arrays

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y = np.array([5, 4, 3, 2, 1])

a, b, c = np.polyfit(x, y, 2)
fig, ax = plt.subplots()
ax.plot(x, a*x)
plt.show()