我正在尝试绘制以下形式的行的.txt文件:filename.txt,date,magnitude。
例如:V098550.txt,362.0,3.34717962317
我试图将日期与幅度进行对比。
我是编码和获取信息的新手:
ValueError:无法将字符串转换为float:V113573.txt,362.0,3.5425960309。
你知道我怎么解决这个问题吗?
import numpy as np
import matplotlib.pyplot as plt
names = '/home/sindelj/research/condensed.txt'
for ii in range (len(names)):
lc = np.loadtxt ("condensed.txt")
plt.scatter (lc[:,0],lc[:,1])
plt.xlabel ('Time')
#take mean date
#date = []
#date_all = numpy.mean(date)
#plt.xlim ([date_all+1, date_all-1])
plt.ylabel ('Mag')
plt.ylim ([15.,14.])
plt.show()# after test comment this out
fileName = names[ii][:-3] + ".png"
plt.savefig(fileName)
print "done"
答案 0 :(得分:0)
根据loadtxt文档,您可以使用usecols
参数指定要加载的列。此外,unpack
参数允许您按列返回数据。
import numpy as np
import matplotlib.pyplot as plt
names = '/home/sindelj/research/condensed.txt'
for ii in range (len(names)):
x, y = np.loadtxt ("condensed.txt", usecols=(1, 2), unpack=True)
plt.scatter (x, y)
plt.xlabel ('Time')
plt.ylabel ('Mag')
plt.ylim ([15.,14.])
plt.show() # after test comment this out
fileName = names[ii][:-3] + ".png"
plt.savefig(fileName)
print "done"