简体   繁体   English

如何在 python 中使用 plt.savefigure() 保存大量图像?

[英]How can i save a lot of images with plt.savefigure() in python?

i wrote the next code:我写了下一个代码:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd 
import pylab as pl 


files = ["xyz_01.txt", "xyz_02.txt", "xyz_03.txt", "xyz_04.txt", "xyz_05.txt", "xyz_06.txt", "xyz_07.txt", "xyz_08.txt", "xyz_09.txt", "xyz_10.txt","xyz_11.txt","xyz_12.txt","xyz_13.txt","xyz_14.txt","xyz_15.txt","xyz_16.txt","xyz_17.txt","xyz_18.txt","xyz_19.txt","xyz_20.txt","xyz_21.txt","xyz_22.txt","xyz_23.txt","xyz_24.txt","xyz_25.txt","xyz_26.txt","xyz_27.txt","xyz_28.txt","xyz_29.txt","xyz_30.txt","xyz_31.txt","xyz_32.txt","xyz_33.txt","xyz_34.txt","xyz_35.txt","xyz_36.txt","xyz_37.txt","xyz_38.txt","xyz_39.txt","xyz_40.txt","xyz_41.txt","xyz_42.txt","xyz_43.txt","xyz_44.txt","xyz_45.txt","xyz_46.txt","xyz_47.txt","xyz_48.txt","xyz_49.txt",]
TB=1
for i in files:
 data=pd.read_csv(i,delim_whitespace=True ,names = ['x', 'y', 'z'])
 T=TB*0.0994
 TB = TB+1 
 fig = plt.figure()
 ax1 = fig.add_subplot(111, projection='3d')
 x=data.iloc[:,0]
 y=data.iloc[:,1]
 z=data.iloc[:,2]
 plt.xlabel("EJE X")
 plt.ylabel("EJE Y")
 pl.plot(x, y, color="blue", linewidth=0, label=str(T))
 pl.legend(loc='upper center',bbox_to_anchor=(0.5, 0.95))

 zLabel = ax1.set_zlabel("EJE Z")
 plt.title("Posición de N partículas en el tiempo[Myr]")
 ax1.scatter(x, y, z, c='b', marker='o')
 plt.savefig()
 plt.show()}

This code create for each element in the list denominated files a graph.此代码为列表命名文件中的每个元素创建一个图表。 Know i need save every graph with plt.savefig() but i don't know how can i automatically assign a different name for each file.知道我需要使用 plt.savefig() 保存每个图形,但我不知道如何为每个文件自动分配不同的名称。 if you can help me i will be deeply grateful.如果你能帮助我,我将不胜感激。

You can make a list consists of your file name.您可以制作一个包含您的文件名的列表。 Or simply put something like 'img'+i+'.jpg' and do a recursion.或者简单地放一些类似'img'+i+'.jpg'并进行递归。

plt.savefig(i[:-4]+'.png')

if you want to name your image according to the file.如果你想根据文件命名你的图像。 i[:-4] is to remove the last 4 characters of i so it will remove .txt from your file name. i[:-4]是删除i的最后 4 个字符,因此它将从您的文件名中删除.txt This solution only works with 3 character extension.此解决方案仅适用于 3 个字符的扩展名。 For general file name, you could use对于一般文件名,您可以使用

filename, extension = os.path.splitext(i)

to split the file name from its extension将文件名与其扩展名分开

You need to think of some rule how to convert each file name to 'png' extension or something of this kind.您需要考虑一些规则,如何将每个文件名转换为 'png' 扩展名或类似的东西。 I face a similar problem where I need to change a file extension many times and the most common solution I use is recipes of os library for file name manipulation.我面临一个类似的问题,我需要多次更改文件扩展名,我使用的最常见的解决方案是用于文件名操作的os库的配方。 Your case is quite easy (as long as you don't have complicated file names that consists of multiple points), this is how I do it using os in general:您的情况很简单(只要您没有包含多个点的复杂文件名),这就是我通常使用os的方式:

import os
file = 'this_is.complicated.txt'
name, extension = os.path.splitext(file)
# name is 'this_is.complicated' now
# extension is '.txt' now

and if we need to construct a new file name, I replace extension like this:如果我们需要构造一个新的文件名,我会像这样替换扩展名:

new_file = name + '.png'
# new_file is 'this_is.complicated.png' now

Of course, you feed this new name to savefig method当然,您将这个新名称提供给savefig方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM