I need to create a figure in a file without displaying it within IPython notebook. I am not clear on the interaction between IPython
and matplotlib.pylab
in this regard. But, when I call pylab.savefig("test.png")
the current figure get's displayed in addition to being saved in test.png
. When automating the creation of a large set of plot files, this is often undesirable. Or in the situation that an intermediate file for external processing by another app is desired.
Not sure if this is a matplotlib
or IPython
notebook question.
This is a matplotlib question, and you can get around this by using a backend that doesn't display to the user, e.g. 'Agg':
EDIT: If you don't want to lose the ability to display plots, turn off Interactive Mode, and only call
plt.show()
when you are ready to display the plots:We don't need to
plt.ioff()
orplt.show()
(if we use%matplotlib inline
). You can test above code withoutplt.ioff()
.plt.close()
has the essential role. Try this one:If you run this code in iPython, it will display a second plot, and if you add
plt.close(fig2)
to the end of it, you will see nothing.In conclusion, if you close figure by
plt.close(fig)
, it won't be displayed.