Sharmila Suri
Sharmila Suri

Reputation: 13

getting 'only length-1 arrays can be converted to Python scalars' error

import matplotlib.pyplot as plt
import numpy as np
from math import sin, pi

y = pi
x = np.linspace(0, 3*y, 500)
plt.plot(x, sin(x**2))
title("A simple Graph")
plt.show()

getting

'only length-1 arrays can be converted to Python scalars'

error while running this code

Upvotes: 0

Views: 6750

Answers (2)

NLMDEJ
NLMDEJ

Reputation: 395

Just change plt.plot(x,sin(x**2)) to plt.plot(x,np.sin(x**2))

Upvotes: 1

Ohumeronen
Ohumeronen

Reputation: 2086

You may use this code instead:

import matplotlib.pyplot as plt
import numpy as np
from math import sin,pi 
y=pi
x = np.linspace(0, 3*y, 500)
plt.plot(x,[sin(_**2) for _ in x])
title("A simple Graph")
plt.show()

The second argument of the plot function was not correct.

Upvotes: 1

Related Questions