My plotting code doesn't seem to be showing the graph (lines 12 to 59 are probably not breaking it, but I included them just in case - I had data that took a while to put into a sorted list).
I've messed around with it and tried different things but I think the main problem is that I don't understand what figure()
, plt.show()
, import matplotlib.pyplot as plt
, from pylab import *
and some other lines actually mean. I don't know when I need to use them or why I need to use them.
Could someone help me and explain how to draw an .svg of two lists and have it show at the end with details about why each line is included and when I put plt.
in front and when I put ax.
in front and when I don't put anything in front, etc? Sorry this will take so long to answer, but I really don't understand matplotlib or any of the examples on their website.
import matplotlib
matplotlib.use('SVG')
import matplotlib.pyplot as plt
import string, math
from pylab import *
from decimal import *
name = raw_input("Enter the filename:\n")
myfile = open("datafiles/"+name+".data", 'r')
xData = []
yData = []
plots = [name]
mydata = myfile.readlines()
i = 0
N = len(mydata)
while (i < N):
string = mydata[i]
data = [str(x) for x in string.split(" ")]
data=filter(lambda x: len(x)>0, data)
xData.append(data[1])
yData.append(data[2])
i = i + 1
i = 0
while (i < N):
if (float(xData[i]) <= 0):
xData[i] = ""
yData[i] = ""
if (xData[i] == "nan" or xData[i] == "-nan"):
xData[i] = ""
yData[i] = ""
i = i + 1
xData=filter(lambda x: len(x)>0, xData)
yData=filter(lambda x: len(x)>0, yData)
N = len(xData)
i = 0
while (i < N):
xData[i] = float(xData[i])
yData[i] = float(yData[i])
i = i + 1
j = 0
while (j < N):
i = 0
while (i < (N-j-1)):
if (xData[i]>xData[i+1]):
a, b = xData[i], xData[i+1]
xData[i+1], xData[i] = a, b
a, b = yData[i], yData[i+1]
yData[i+1], yData[i] = a, b
i = i + 1
j = j + 1
plot = plt.figure(1)
plt.plot(xData, yData)
plt.show(1)
plt.savefig(name)
Ok, so The answer Is very simple. (Ubuntu 21.10)
you will need to install some version of PyQt. At the moment:
pip install PyQt6
import matplotlib
matplotlib.use("QtAgg")
plt.show()
from matplotlib import pyplot as plt
# some code here
plt.show()
This worked flawlessly for me. Hope It worked for you too.
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
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With