我有这个代码从文件夹中的所有文本文件生成多个图.它运行得很好,并显示了情节,但我不知道如何保存所有.
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()
Run Code Online (Sandbox Code Playgroud)
我以前用过
fileName="C:\documents\FirstPlot.png"
plt.savefig(fileName, format="png")
Run Code Online (Sandbox Code Playgroud)
但我认为这只是将每个图表保存到一个文件中并覆盖最后一个.
我需要对表的第一列进行排序.它看起来像
6000 799
7000 352
8000 345
9000 234
10000 45536
11000 3436
1000 342
2000 123
3000 1235
4000 234
5000 233
Run Code Online (Sandbox Code Playgroud)
我希望第一列按升序排列,但它只按第一个数字排序,而不是整列的值,即
1000 342
10000 45536
11000 3436
2000 123
Run Code Online (Sandbox Code Playgroud)
但我想要
1000 342
2000 123
3000 1235
etc
Run Code Online (Sandbox Code Playgroud)
目前尝试:
SortInputfile=open("InterpBerg1","r")
line=SortInputfile.readlines()
line.sort()
map(SortOutputfile.write, line)
Run Code Online (Sandbox Code Playgroud) 运行以下代码以将文本文件的列中的值与给定数字进行比较,在本例中为440
with open('test.txt', 'a+') as input:
for line in input:
columns = line.split(" ")
print columns[5] #test
if columns[5] == '440':
print 'match'
Run Code Online (Sandbox Code Playgroud)
test.txt只是:
0 0 0 0 0 1
0 0 0 0 0 440
0 0 0 0 0 1
0 0 0 0 0 440
0 0 0 0 0 1
0 0 0 0 0 1
Run Code Online (Sandbox Code Playgroud)
打印列[5]位从txt文件打印出正确的值,但即使它匹配440,for循环中的if也不起作用
感谢您的任何帮助