简体   繁体   中英

Graphing in python using numpy

I'm new at python so I'm probably missing something obvious

But I want to import data from excel and graph it. In excel there is one column with each row having a list of numbers separated by commas. I want to extract the second and third numbers from each array to use as x and y.

I've converted the excel file to a csv file.

fname = str("Raw1.csv") # store in data directory
data = np.loadtxt('../data/' + fname,
                delimiter=',',
                skiprows=6,
                usecols=(1,2,6,7,8,3,4,5)) 

x1 = data[:,[1]]
print x1
y1 = data[:,[2]]
print y1

But when i check that I've extracted the correct data, x1 shows the third number and y1 defaults to zero.

I'm not sure why this is happening?

Here's some example data from the csv file

"0, 57217.09, 51514.46, 58537.72, 44444.79, 0.00, 0.00, 0.00, 0.00, 0.016, 
0.472, 0.170, 14:22:39.438"
"1, 56912.86, 51240.30, 58259.08, 44179.38, 0.00, 0.00, 0.00, 0.00, 0.000, 
0.000, 0.000, 14:22:39.919"
"2, 58354.95, 52950.70, 59965.86, 45914.75, 0.00, 0.00, 0.00, 0.00, 0.000, 
0.000, 0.000, 14:22:39.920"

使用np.shape()查找数据的维数,然后您可以清楚地看到实际的错误是什么,并根据需要从列表中提取数据。

From numpy docs

usecols : Which columns to read, with 0 being the first. For example, usecols = (1,4,5) will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read.

So in your data, you have 2nd, 3rd, 7th and so on columns, data[:,[1]] is really the third number, data[:,[2]] is then 7th number (is it 0?)

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