Reputation: 17
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
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:
Whereas if you were to pass in floats between 0
and 2 * math.pi
, you would get a nice sine wave:
Upvotes: 7