import numpy as np
import matplotlib.pyplot as plt
D = 12
n = np.arange(1,4)
x = np.linspace(-D/2,D/2, 3000)
I = np.array([125,300,75])
phase = np.genfromtxt('8phases.txt')
I_phase = I*phase
for count,i in enumerate(I_phase):
F = sum(m*np.cos(2*np.pi*l*x/D) for m,l in zip(i,n))
f = plt.figure()
ax = plt.plot(x,F)
plt.savefig(str(count)+'.png')
plt.show()
此脚本生成8个图并保存它们。我想给所有情节提供不同的标题。它是否有可能读取.txt
或Excel文件(.xls
)并直接从那里获取每个情节的标题?例如;我有这样的标题(可以保存为.txt
或.xls
文件):
phase_01_water
phase_02_membrane
phase_03_water
phase_04_empty
phase_05_water
phase_06_water
phase_07_full
phase_08_water
我该怎么做? '8phases.txt'
有以下8行:
-1 1 -1
-1 1 1
1 1 1
1 -1 1
-1 -1 -1
1 1 -1
1 -1 -1
-1 -1 1
答案 0 :(得分:2)
如果标题位于您的示例中的简单txt文件中,您可以使用类似的内容加载它们
with open('titles_file.txt') as f:
titlelist = f.readlines()
然后matplotlib.Axes
有一个方法set_title
。我还以更面向对象的方式重写你的for循环
for count,i in enumerate(I_phase):
F = sum(m*np.cos(2*np.pi*l*x/D) for 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')