如何使用for循环绘制使用matplotlib保存到不同文件的图形? [英] How to use for loop to plot figures saved to different files with matplotlib?

查看:698
本文介绍了如何使用for循环绘制使用matplotlib保存到不同文件的图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过for循环绘制n个独立的图形,并将每个图形保存到一个文件中.我的代码如下:

I wanted to plot n independent figures by a for loop, with each figure saved to one file. My code is as following:

import matplotlib.pyplot as plt
import numpy as np
for i in range(len(nfile)): #nfile is a list of file names
    data = np.load(nfile[i])
    plt.plot(data[0], data[1])
    plt.savefig("figure_%d.png"%i, dpi=300)

我只希望data [i]的图显示在fig_i.png中,但是以前的图(j = 0,...,i-1)也显示在fig_i.png中.有什么办法解决这个问题?

I wanted only the plotting of data[i] to show in figure_i.png, but the former plottings (j=0, ..., i-1) also showed in figure_i.png. Is there any way to solve this?

非常感谢!

推荐答案

在循环的开头,添加:

plt.close()

这篇关于如何使用for循环绘制使用matplotlib保存到不同文件的图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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