Matplotlib Plt.show() Isn't Showing Graph
Solution 1:
You are attempting to use a backend that will not produce graphics with plt.show()
. Instead you need to use another backend such as WXAgg or QT4agg, the selection of which will depend on your system. See this information on Matplotlib's backends. Instead, you should use only plt.savefig('filename.svg')
if you desire to have a file in the svg format. The resulting file will be in your working directory, you only need to open it after your script has finished.
To elaborate a bit to answer some of your other questions about not understanding what individual lines mean:
plt.show()
will produce an interactive plot on your screen, assuming you are using a backend (renderer) that supports plotting to your user interface.
import matplotlib.pyplot as plt
simply imports the functions and classes from the pyplot library from the matplotlib package, and the as plt part is sort of like making a nickname to access those functions easier. For example, plt.show()
, plt.figure
, etc. instead of having to type out matplotlib.pyplot.show()
every time. On the other hand, from pylab import *
imports all of the functions without the prefix. In general, I would avoid using import *
because it can be confusing to read back your code later. Also, pylab probably isn't something you need for the code you've shown here.
plt.figure()
is the command that initializes your figure. In this case, since you used plot = plt.figure
, you can then type plot.plot(xData, yData)
, because your variable plot now is part of the figure class. You would use ax
for example if you had some additional axes, subplots or color bars on which you needed to perform some action.
I would really recommend going through the pyplot tutorial on the matplotlib website to give you a more thorough, but still relatively brief and simple introduction to using matplotlib.
Solution 2:
If you did pip install matplotlib
in a virtualenv with --no-site-packages
, and plt.show()
isn't showing up your plot:
1) Either apt-get
install matplotlib, then virtualenv --system-site-packages FOLDERNAME
2) Or, from this guide:
pip uninstall matplotlib
sudo apt-get install python-gtk2-dev
ln -sf /usr/lib/python2.7/dist-packages/{glib,gobject,cairo,gtk-2.0,pygtk.py,pygtk.pth} $VIRTUAL_ENV/lib/python2.7/site-packages
pip install matplotlib
There is still another step in the guide, but that wasn't neccessary for me (set the backend to GTKAgg
in ~/.config/matplotlib/matplotlibrc
)
Solution 3:
Im my case I resolved the problem by installing python 3.7 from python.org and then simply changed interpreter on my IDE (PyCharm CE) + of course installed all the necessary packages again. There was no need to use another backend and I kept TkAgg in my project.
Solution 4:
Try this: -
plot = plt.figure()
plt.plot(xData, yData)
plt.show()
plt.savefig(str(name).split()[0]+'.png)
and please try no to take name as input from the user
Post a Comment for "Matplotlib Plt.show() Isn't Showing Graph"