1

Get this error when trying to plot a function with respect for a range of x values

TypeError: unsupported operand type(s) for *: 'float' and 'range'

import numpy as np
import matplotlib.pyplot as plt
x = range(273, 1273)
print(list(x))
y = -0.7765 + (0.014350 * x) - (0.000012209 * (x ** 2)) + (3.8289e-09 * (x ** 3))
plt.plot(x, y, 'r')
plt.show()
2
  • 1
    use np.arange instead of range Commented Sep 29, 2020 at 15:18
  • 1
    A side note: range returns a generator, which is consumed when you do print(list(x)). That means you cannot use it again later in plt.plot(x,y,'r'). Commented Sep 29, 2020 at 15:30

1 Answer 1

1

When you use the function range, it uses python's range function which cannot be used in arithmetic directly as it is an iterator. So you get an error saying multiplication is not supported for: range and float.

When you use NumPy's arange, it has an inbuilt ability to handle such arithmetic. Hence, your code should be using that.

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(273, 1273) # This
y = -0.7765 + (0.014350 * x) - (0.000012209 * (x ** 2)) + (3.8289e-09 * (x ** 3))
plt.plot(x, y, 'r')
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.