2017-06-10 84 views
0

我想繪製一個.txt格式的文件:filename.txt,日期,大小。例如:V098550.txt,362.0,3.34717962317我正在試圖對照這個數量繪製日期。繪製一個.txt文件,麻煩轉換字符串浮動

我是新來編碼和獲取消息:ValueError:無法將字符串轉換爲浮動: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

你可以使用'np.loadtxt跳過第一列( 「condensed.txt」,usecols =(1,2))' – taras

+0

@ user128285你能提供一個答案您的評論,可以接受? – ImportanceOfBeingErnest

回答

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"