简体   繁体   中英

Plotting a .txt file, trouble converting string to float

I am trying to plot a .txt file of lines of the form: filename.txt,date,magnitude.

Ex: V098550.txt,362.0,3.34717962317

I am trying to plot the date against the magnitude.

I am new to coding and getting the message:

ValueError: could not convert string to float: V113573.txt,362.0,3.5425960309.

Do you know how I can fix this?

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"

According to loadtxt docs you can specify which columns to load with usecols argument. Also unpack argument allows you to return data columnwise.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM