Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use savefig in Python with string and iterative index in the name

I need to use the "savefig" in Python to save the plot of each iteration of a while loop, and I want that the name i give to the figure contains a literal part and a numerical part. This one comes out from an array or is the number associated to the index of iteration. I make a simple example:

# index.py

from numpy import *
from pylab import *
from matplotlib import *
from matplotlib.pyplot import *
import os

x=arange(0.12,60,0.12).reshape(100,5)
y=sin(x)

i=0

while i<99
  figure()
  a=x[:,i]
  b=y[:,i]
  c=a[0]
  plot(x,y,label='%s%d'%('x=',c))

  savefig(#???#)      #I want the name is: x='a[0]'.png
                      #where 'a[0]' is the value of a[0]

thanks a lot.

like image 926
user1872346 Avatar asked Dec 03 '12 11:12

user1872346


People also ask

What is the use of Savefig () Explain with example?

savefig() As the name suggests savefig() method is used to save the figure created after plotting data. The figure created can be saved to our local machines by using this method. Filename .

How do I use Savefig?

Saving a plot on your disk as an image file savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

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

Well, it should be simply this:

savefig(str(a[0]))

This is a toy example. Works for me.

import pylab as pl
import numpy as np

# some data
x = np.arange(10)

pl.figure()
pl.plot(x)
pl.savefig('x=' + str(10) + '.png')
like image 194
blueSurfer Avatar answered Oct 22 '22 18:10

blueSurfer