我有這段代碼可以從文件夾中的所有文本文件生成多個地塊。它運行得非常好,顯示情節,但我不能解決如何將它們全部保存。保存多個地塊
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")
,但我認爲這只是保存每個圖形到一個文件中,並覆蓋上一個。
嗨,謝謝你的幫助。我嘗試過這種方法,它的所有工作,但情節都出來了空白。我也使用了pl.show(),並且他們生成了正確的繪圖,而不是實際的節省位。有任何想法嗎? – user1841859
@ user1841859:我不知道。在保存之前,可能需要'pl.show()'?我自己並沒有使用過'pylab'。 –
plt.show()不能來之前plt.savefig 您必須先保存它,然後再顯示它。 – arynaq