Subarna Lamsal
Subarna Lamsal

Reputation: 17

I am not able to create the graph for y=sin(x)

enter code here
import numpy as np  
import math  
import matplotlib.pylab as plt  
a=np.linspace(3,6,10)  
plt.plot(a,math.sin(a))  
plt.show()

The output says ****TypeError: only size-1 arrays can be converted to Python scalars

Upvotes: 1

Views: 332

Answers (1)

Joe Iddon
Joe Iddon

Reputation: 20414

Use np.sin or np.vectorize(math.sin).


import numpy as np  
import math  
import matplotlib.pylab as plt  
a = np.linspace(3,6,10)  
plt.plot(a, np.sin(a))  
plt.show()

Note that np.sin, like math.sin, takes radians rather than degrees, so you may want to adjust your array (a) accordingly, or use np.rad2deg because at the moment the result is:

plt1

Whereas if you were to pass in floats between 0 and 2 * math.pi, you would get a nice sine wave:

plt2

Upvotes: 7

Related Questions