I have a small graph plotting code written in python using matplotlib and the graph is not being displayed output. The script works fine. I have added print statements at the end and it works fine, however the graph doesn't show up. Can you please help?
Following is the code snippet I have used for plotting:-
import matplotlib.pyplot as plt
plt.title("BPD")
ax.set_xlabel('D')
ax.set_ylabel('P')
plt.savefig("DP.png")
plt.pause(5)
plt.show(block== False)
plt.close()
Remove plt.pause(5), plt.show(block== False) and plt.close() from your code
Related
Just a small question. I have VS Code installed and trying it out with Python but no matter what I try I cannot get matplotlib plots to appear.
Here is a simple code that does NOT work
import mglearn
import matplotlib.pyplot as plt
X, y = mglearn.datasets.make_wave(n_samples=40)
plt.plot(X, y, 'o')
plt.ylim(-3, 3)
plt.xlabel("Feature")
plt.ylabel("Target")
No error with the code appears but also no plot. Thanks.
Please note mglearn comes from the following Github
https://github.com/amueller/mglearn
Try plt.show() at the end.
And this additional line is just because the system asks me extra text for no reason.
Your code works.
You need to ask for the picture to be shown plt.show() or to be saved plt.savefig().
Just add plt.show() and you will obtain:
I want to create a log-log plot using pyplot, but have trouble when calling plt.show():
import matplotlib.pyplot as plt
xVec = [...]
yVec = [...]
plt.figure()
plt.loglog(xVec,yVec,'.',label='This is my test plot')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.show()
I am running this code from C++ via:
Py_Initialize();
Py_SimpleString(pythonCode.str().c_str());
Py_Exit(0);
where pythonCode is a stringstream containing the Python code above. The code runs if I don't include the plt.show() line, but of course no plot shows up.
The matplotlibrc config file shows that the backend is TkAgg, which shouldn't give problems as indicated here or here. I've tried adding plt.close() after the last line in the code above, but the error persists.
Perhaps the most surprising thing is this: I've also tried running the code in a separate Python script (with plt.show()), and the plot appears correctly! Does anyone have any idea about what's going on? Thanks in advance!
EDIT: I have also tried pylab instead of pyplot, with the same results. Do I need to compile the program with a certain python module to link the libraries properly?
I keep trying to follow the examples I see for PdfPages but keep getting the value error: No such figure: None.
plot1 = Chart Generating Function(argument1, argument2,...)
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('sample.pdf')
pp.savefig(plot1)
plt.close()
I've tried different variations of this (i.e. pdf.savefig()) but nothing seems to work.
What solved the problem for me was removing the plt.show() command at the end of my chart generating function.
I should have added more details, but somewhere in my code I had used "fig, ax = ..." when defining the figure. The "fig" part needed to be the argument in pdf.savefig(fig) in order for it to work.
In the past I was able to do simple animations with matplotlib with a for loop, but this hasn't worked for some time now.
The standard answer is that you have to turn interactive mode on and/or force a redraw with matplotlib.pyplot.draw(). Here is my minimal working example:
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as mplot
mplot.ion()
fig = mplot.figure(1)
ax = fig.add_subplot(111)
for ii in np.arange(0,10):
x = 200*np.random.rand(30)
ax.plot(x)
mplot.draw()
filename = ("img_%d.png" % ii)
mplot.savefig(filename)
When I run this in Interactive Python Editor, I get one figure at the very end with all the plots in it (this also happens with mplot.show())
When I run this in IPython 3.1 (with Python 3.3.5) from the command line, I get nothing at all.
The mplot.savefig(filename) line does seem to work, as the images are generated.
(It's possible this is a bug in the Qt4 backend.)
Try deleting the line matplotlib.use('Qt4Agg'). Works for me. Also works with matplotlib.use('TkAgg'). So it is a backend problem. There is another way to do animations.
I have a strange problem with matplotlib that I can not seem to figure out. When using the ipython notebook with the pylab flag, ipython notebook --pylab inline I have a line of code that looks like this that is used to generate a colorbar with matplotlib:
im = ax.imshow(df, vmin=vmin, vmax=vmax)
The code works correctly and I get a nice colorbar. When I run this code as a python file I get an error, NameError: name 'ax' is not defined. I understand that the ipython notebook --pylab inline automatically imports a bunch of stuff into the notebook, but I cannot figure out what I need to import to fix the problem. print type(ax) gives:
<class 'matplotlib.axes.AxesSubplot'>
Can anyone point out why my code works in ipython but not a plain python file? Thanks in advance.
I had the same problem.
Per this entry:
How to abbreviate xtick labels years to 2 digits in a matplotlib plot
try defining 'ax' by adding (before the line causing the error):
ax = plt.gca()
I'm not quite sure what you've done, because aX isn't defined by default as part of pylab.
Normally, ax refers to an axis object. There are a few ways you can get one:
matplotlib.pyplot.gca() # gca = get current axis
matplotlib.pyplot.subplot(2,1,1) # For creating multiple plots in one figure
fig.get_axes()[x] # Where fig is a Figure object