加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:55:14 所属栏目:Python 来源:网络整理
导读:我需要在Python中使用“savefig”来保存while循环的每次迭代的图,我希望我给图的名称包含一个文字部分和一个数字部分.这个是从数组中出来的,或者是与迭代索引相关联的数字.我举一个简单的例子: # index.pyfrom numpy import *from pylab import *from matpl

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

# 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]

非常感谢.

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

savefig(str(a[0]))

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

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')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读