保存多个地块

编程入门 行业动态 更新时间:2024-10-14 20:23:35
本文介绍了保存多个地块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这段代码可以从文件夹中的所有文本文件生成多个图表.它运行得非常好,并显示了情节,但是我不知道如何保存所有情节.

i have this code to produce multiple plots from all the text files in a folder. It runs perfectly fine and shows the plots but i cant work out how to then save them all.

import re import numpy as np import matplotlib.pyplot as plt import pylab as pl import os rootdir='C:\documents\Neighbors for each search id' for subdir,dirs,files in os.walk(rootdir): for file in files: f=open(os.path.join(subdir,file),'r') print file data=np.loadtxt(f) #plot data pl.plot(data[:,1], data[:,2], 'gs') #Put in the errors pl.errorbar(data[:,1], data[:,2], data[:,3], data[:,4], fmt='ro') #Dashed lines showing pmRa=0 and pmDec=0 pl.axvline(0,linestyle='--', color='k') pl.axhline(0,linestyle='--', color='k') pl.show() f.close()

我以前用过

fileName="C:\documents\FirstPlot.png" plt.savefig(fileName, format="png")

但是我认为这只是将每个图形保存到一个文件中并覆盖最后一个文件.

but i think this just saves each graph into one file and overwrites the last one.

推荐答案

您要做的就是提供唯一的文件名.您可以使用一个计数器:

All you have to do is provide unique filenames. You could use a counter:

fileNameTemplate = r'C:\documents\Plot{0:02d}.png' for subdir,dirs,files in os.walk(rootdir): for count, file in enumerate(files): # Generate a plot in `pl` pl.savefig(fileNameTemplate.format(count), format='png') pl.clf() # Clear the figure for the next loop

我做了什么:

  • 使用python的字符串格式语法

使用 enumerate()函数<.

使用计数器和模板为每个图生成新的文件名.

Used the counter and the template to generate a new filename for each plot.

更多推荐

保存多个地块

本文发布于:2023-05-29 03:36:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/335852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   地块

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!