. Advertisement .
..3..
. Advertisement .
..4..
Hello everyone! Today we will continue to learn a new case. Have you ever encountered the “name ‘plt’ not defined” problem when using IPython? This is a pretty common mistake, especially with newbies. There are many ways to handle this problem, the following article will also give you the fastest solution. Let’s keep track!
How Does The Error “name ‘plt’ not defined” occur?
Below is the error message returned by the console:
NameError Traceback (most recent call last)
< ipython-input-1-1eb00ff78cf2> in <module>
----> 1 plt.show()
NameError: name 'plt' is not defined
The above situation occurs when the user is running Hydrogen. Matplotlib and IPython still run normally and no errors occur. This is a program created to run tests:
%matplotlib ipympl
import matplotlib.pyplot as plt
a_x=[1,2,3,4,5,6]
a_y=[1,2,3,4,5,6]
plt.plot(a_x, a_y)
plt.show()
You can refer to the error video in this link to better understand the error situation.
What causes error?
The main reason to cause the above error is that when you start running the program, it did not run the whole thing, but only ran 1 or some commands.
Solution
After watching the video of the above error, you can immediately recognize the cause of your error. When you click run Hydrogen program, if at that command line and you only select “Run”, the system will only run each of your commands and that is the cause of your error. For the above situation, instead of just selecting “Run”. Please select “Run All” to run the entire program instead of just one command. Now run it again to check if your program is working properly or not.
Alternatively, you can use the following program:
import matplotlib.pyplot as plt
X = [[6], [8], [10], [14], [18]]
y = [[7], [9], [13], [17.5], [18]]
plt.figure()
plt.title('Pizza price plotted against diameter')
plt.xlabel('Diameter in inches')
plt.ylabel('Price in dollars')
plt.plot(X, y, 'k.')
plt.axis([0, 25, 0, 25])
plt.grid(True)
plt.show()
Conclusion
Our above article has shown the wrong operation that causes your program to encounter the “name ‘plt’ not defined” error while using IPython. Please check the program and follow the instructions so that your project can work properly again. Hopefully with this guide you will have a better understanding of the problem so that you can draw new lessons for other projects. If you have any questions or difficulties, do not hesitate to contact us immediately for answers.
Our website is always updated and offers ways to handle errors for programmers encountered. In addition, this is also a useful site for free programming materials for everyone. Check back often so you can find information you may be interested in! Thank you for reading.
Leave a comment