如何使用Python将多个图保存在一个文件夹中? [英] How could I save multiple plots in a folder using Python?

查看:1144
本文介绍了如何使用Python将多个图保存在一个文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的python程序,我试图将多个图保存在一个文件夹中,但似乎不起作用.请问我该怎么做?

Here is my program in python and I am trying to save multiple plots in a single folder but it doesn't seem to work. How could I do this please?

for i in range(0:244):
plt.figure()
y = numpy.array(Data_EMG[i,:])
x = pylab.linspace(EMG_start, EMG_stop, Amount_samples)
plt.xlabel('Time(ms)')
plt.ylabel('EMG voltage(microV)')
pylab.plot(x, y)
pylab.show(block=True)

推荐答案

首先检查身份.希望您的代码实际读取

First of all check the identation. Hopefully your code actually reads

for i in range(0:244):
    plt.figure()
    y = numpy.array(Data_EMG[i,:])
    x = pylab.linspace(EMG_start, EMG_stop, Amount_samples)
    plt.xlabel('Time(ms)')
    plt.ylabel('EMG voltage(microV)')
    pylab.plot(x, y)
    pylab.show(block=True)

在每次迭代中,您都会完全生成一个新图形.那是非常无效的.另外,您只是在屏幕上绘制图形而不实际保存它.更好的是

At each iteration you completely generate a new figure. That´s very ineffective. Also you just plot your figure on the screen and not actually save it. Better is

from os import path
data = numpy.array(Data_EMG)                 # convert complete dataset into numpy-array
x = pylab.linspace(EMG_start, EMG_stop, Amount_samples) # doesn´t change in loop anyway

outpath = "path/of/your/folder/"

fig, ax = plt.subplots()        # generate figure with axes
image, = ax.plot(x,data[0])     # initialize plot
ax.xlabel('Time(ms)')
ax.ylabel('EMG voltage(microV)')
plt.draw()
fig.savefig(path.join(outpath,"dataname_0.png")

for i in range(1, len(data)):
    image.set_data(x,data[i])
    plt.draw()
    fig.savefig(path.join(outpath,"dataname_{0}.png".format(i))

应该更快.

这篇关于如何使用Python将多个图保存在一个文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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