python - Giving different titles to plots in for-loop -
this question has answer here:
import numpy np import matplotlib.pyplot plt d = 12 n = np.arange(1,4) x = np.linspace(-d/2,d/2, 3000) = np.array([125,300,75]) phase = np.genfromtxt('8phases.txt') i_phase = i*phase count,i in enumerate(i_phase): f = sum(m*np.cos(2*np.pi*l*x/d) m,l in zip(i,n)) f = plt.figure() ax = plt.plot(x,f) plt.savefig(str(count)+'.png') plt.show() this script generates 8 plots , saves them. want give plots different titles. possible read .txt or excel file (.xls) , take title directly there each plot? example; have titles these (can saved either .txt or .xls file):
phase_01_water phase_02_membrane phase_03_water phase_04_empty phase_05_water phase_06_water phase_07_full phase_08_water how can that? '8phases.txt' has following 8 lines:
-1 1 -1 -1 1 1 1 1 1 1 -1 1 -1 -1 -1 1 1 -1 1 -1 -1 -1 -1 1
you can if titles in simple txt file in example, can load them this
with open('titles_file.txt') f: titlelist = f.readlines() then matplotlib.axes have method set_title. rewrite loop in more object oriented way
for count,i in enumerate(i_phase): f = sum(m*np.cos(2*np.pi*l*x/d) m,l in zip(i,n)) fig = plt.figure() ax = fig.add_subplot(111) ax.set_title(titlelist[count]) ax.plot(x,f) fig.savefig(str(count)+'.png')
Comments
Post a Comment