在名称中使用字符串和迭代索引的 Python 中的 savefig [英] Use savefig in Python with string and iterative index in the name

查看:29
本文介绍了在名称中使用字符串和迭代索引的 Python 中的 savefig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]

非常感谢.

推荐答案

嗯,应该是这样的:

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

这篇关于在名称中使用字符串和迭代索引的 Python 中的 savefig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆