Just getting into programming by going through a load of beginner projects. I started one to plot sine and cosine curves, and the code they gave was:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 4*np.py, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
Every time I try and run the code, it gives the error:
Traceback (most recent call last):
File "C:/Users/Alex/PycharmProjects/projects2/sin2.py", line 5, in <module>
x = np.arange(0, 4*np.py, 0.1)
File "C:\Users\Alex\anaconda3\envs\projects2\lib\site-packages\numpy\__init__.py", line 220, in __getattr__
"{!r}".format(__name__, attr))
AttributeError: module 'numpy' has no attribute 'py'
I've reinstalled Python, pycharm, and numpy to no avail. I believe I'm properly using the anaconda interpreter and I see numpy is properly installed on it. I'm not sure what else I should try, so any suggestions would help. Maybe I should try another IDE? I do like Pycharm so far but I have seen other people with similar issues using Pycharm, so any suggestions there would also be welcome.
I think you are trying to get np.pi The constant 3.1415926535897932384626433...
If you change it in your code:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 4 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
It properly runs
I not very familiar with numpy, but I looked through the docs and couldn't find anything for py being part of numpy. Maybe I didn't look hard enough, but it could have been replaced with an update? Also, you could try changing it to "np.py()" with parentheses because it could just be a function instead (however that is probably not likely assuming you got this from a prebuilt project).
I don't know, why are you using 4*np.py , I have seen the documentation of NumPy and it has no such attribute as py. You can try following:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.title('sine wave') # To give a title
plt.xlabel('Time') # To give a x-label
plt.ylabel('Y-values') # To give a y-label
plt.grid(True, which='both') # Turns Grid to True
plt.axhline(y=0, color='k') # Draw a black horizontal line at y=0
plt.show()
Output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.