在Python中使用savefig,名称中包含字符串和迭代索引

前端之家收集整理的这篇文章主要介绍了在Python中使用savefig,名称中包含字符串和迭代索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要在Python中使用“savefig”来保存while循环的每次迭代的图,我希望我给图的名称包含一个文字部分和一个数字部分.这个是从数组中出来的,或者是与迭代索引相关联的数字.我举一个简单的例子:

  1. # index.py
  2. from numpy import *
  3. from pylab import *
  4. from matplotlib import *
  5. from matplotlib.pyplot import *
  6. import os
  7. x=arange(0.12,60,0.12).reshape(100,5)
  8. y=sin(x)
  9. i=0
  10. while i<99
  11. figure()
  12. a=x[:,i]
  13. b=y[:,i]
  14. c=a[0]
  15. plot(x,y,label='%s%d'%('x=',c))
  16. savefig(#???#) #I want the name is: x='a[0]'.png
  17. #where 'a[0]' is the value of a[0]

非常感谢.

最佳答案
嗯,它应该是这样的:

  1. savefig(str(a[0]))

这是一个玩具的例子.适合我.

  1. import pylab as pl
  2. import numpy as np
  3. # some data
  4. x = np.arange(10)
  5. pl.figure()
  6. pl.plot(x)
  7. pl.savefig('x=' + str(10) + '.png')

猜你在找的Python相关文章