31

I have a CSV file at e:\dir1\datafile.csv. It contains three columns and 10 heading and trailing lines need to be skipped. I would like to plot it with numpy.loadtxt(), for which I haven't found any rigorous documentation.

Here is what I started to write from the several tries I found on the web.

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook

def read_datafile(file_name):
    # the skiprows keyword is for heading, but I don't know if trailing lines
    # can be specified
    data = np.loadtxt(file_name, delimiter=',', skiprows=10)
    return data

data = read_datafile('e:\dir1\datafile.csv')

x = ???
y = ???

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax1.set_title("Mains power stability")    
ax1.set_xlabel('time')
ax1.set_ylabel('Mains voltage')

ax1.plot(x,y, c='r', label='the data')

leg = ax1.legend()

plt.show()
bmu
  • 35,119
  • 13
  • 91
  • 108
user1850133
  • 2,833
  • 9
  • 29
  • 39

2 Answers2

47

According to the docs numpy.loadtxt is

a fast reader for simply formatted files. The genfromtxt function provides more sophisticated handling of, e.g., lines with missing values.

so there are only a few options to handle more complicated files. As mentioned numpy.genfromtxt has more options. So as an example you could use

import numpy as np
data = np.genfromtxt('e:\dir1\datafile.csv', delimiter=',', skip_header=10,
                     skip_footer=10, names=['x', 'y', 'z'])

to read the data and assign names to the columns (or read a header line from the file with names=True) and than plot it with

ax1.plot(data['x'], data['y'], color='r', label='the data')

I think numpy is quite well documented now. You can easily inspect the docstrings from within ipython or by using an IDE like spider if you prefer to read them rendered as HTML.

bmu
  • 35,119
  • 13
  • 91
  • 108
  • 2
    Thanks for your information. I downloaded the numpy ref. manual; it has all the information I need about genfromtxt. – user1850133 Dec 02 '12 at 19:51
  • nice to hear. On Stackoverflow you would normally upvote an answer, if it was useful for you (and maybe leaving a comment) and / or accept it if your question is answered. – bmu Dec 02 '12 at 20:57
5

I'm guessing

x= data[:,0]
y= data[:,1]
Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • 1
    Or, alternately, `x, y = data[:,:2].T` (or even `x, y, third_column = data.T` if `data` will always have 3 columns). – Joe Kington Nov 25 '12 at 02:04