简体   繁体   中英

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. 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.

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. This solution only works with 3 character extension. 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. 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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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