Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my plt.savefig is not working?

I have a simple python code as follows:

import numpy as np import matplotlib.pyplot as plt  """ Here are the solutions and the plot. """  # Create the axis and plot. plt.axis([0, 10, 0, 10]) axis_x = range(1, 11) grd = [1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1, 10.1] grd2 = [1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2, 8.2, 9.2, 10.2] plt.plot(axis_x, grd, '-g', label='BR1') plt.plot(axis_x, grd2, '-b', label='BR2') plt.legend(loc='upper left') plt.grid() plt.show()  # Save the results vector to a text file. np.savetxt('test.out', (grd, grd2))  # Save the figure as '.eps' file. plt.savefig('expl.pdf', format='pdf', dpi=1200) 

When I open the output files expl.pdf and/or test.out I find them blank and nothing in there. Why?

Thanks.

like image 727
Jika Avatar asked Jun 10 '15 19:06

Jika


People also ask

How do I use Savefig in matplotlib?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

Why is matplotlib not working?

Occasionally, problems with Matplotlib can be solved with a clean installation of the package. In order to fully remove an installed Matplotlib: Delete the caches from your Matplotlib configuration directory. Delete any Matplotlib directories or eggs from your installation directory.

How do you save a figure in PNG format in Python?

To save plot figure as JPG or PNG file, call savefig() function on matplotlib. pyplot object. Pass the file name along with extension, as string argument, to savefig() function.


1 Answers

When you close the image displayed by plt.show(), the image is closed and freed from memory.

You should call savefig and savetxt before calling show.

like image 84
shx2 Avatar answered Nov 07 '22 03:11

shx2