Home > Net >  Can't multiply sequence by non-int
Can't multiply sequence by non-int

Time:05-11

The following code is the minimum example of a error that I'm not sure how to solve it.

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)

What am I doing wrong?

My jupyter notebook

CodePudding user response:

You were trying to multiply a list with a non-integer. Instead, use 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()

Plot

  • Related