1

How can I save a plot in a png image? In my code i image the plot like this:

plt.title(('FRC_Network_Coordinates_ID_{}_r_{}_x_{}_y_{}_z_{}_fcr_{}_co_{}_dt_{}.csv'.format(ID, r, x, y, z, fcr_size,co,dt)))
plt.xlabel('Threshold')
plt.ylabel('Jacaard Coefficient')
plt.plot(np.arange(0,1.1,0.05),JClist)
plt.grid(True)
plt.show()

How can I now store the result of the plot in an image (png)? And is it also possible to store the plots in a list and save all plots in one png or pdf if i for example would have a for loop which creates multiple plots one after another.

Ron Lauterbach
  • 107
  • 1
  • 1
  • 12
Varlor
  • 1,421
  • 3
  • 22
  • 46
  • Also see https://stackoverflow.com/questions/1358977/how-to-make-several-plots-on-a-single-page-using-matplotlib/2060170#2060170 for how to add multiple subplots to an image – tmdavison Jun 24 '17 at 12:24
  • But i dont have a "figure" – Varlor Jun 24 '17 at 12:24
  • Yes you do, `plt.plot` will have created a figure instance, in which there is an `axes` instance, upon which your line plot is shown. `plt.savefig()` will save the "current" figure, i.e. the last one to be operated on by `matplotlib` – tmdavison Jun 24 '17 at 12:26
  • If you want to get a reference to your figure, you can either create it before you plot with `fig=plt.figure()`, or you can **g**et the **c**urrent **f**igure after you call `plt.plot` using `fig=plt.gcf()` – tmdavison Jun 24 '17 at 12:28
  • Ok, but then i get this error: TypeError: Object does not appear to be a 8-bit string path or a Python file-like object – Varlor Jun 24 '17 at 12:39
  • did you give `savefig` a filename to save to, as shown in the duplicate question? (e.g. `plt.savefig('myfig.png')`) – tmdavison Jun 24 '17 at 12:42
  • Ah! with fig.savefig('myfig.png') it works! Thank you a lot! :) – Varlor Jun 24 '17 at 12:47

0 Answers0