3

I have a data file where I want to plot specific lines of the second column. My script is as follows:

f=open('datafile','r')
lines1=f.readlines()[4:24]#since I want the values from the 4th to the 23rd line
lines2=f.readlines()[33:54]#I want the values from the 33rd to the 53rd line
f.close()

x1=[]
y1=[]

for line in lines1:
    p=line.split()
    x1.append(float(p[1]))#the values are in the second column
for line in line2:
    p=line.split()
    y1.append(float(p[1]))

xv=np.array(x1)
yv=np.array(y1)

plt.plot(xv,yv)

However, at the end I have an error saying "x and y must have same first dimension". I am not very experienced with python, could somebody advise me any alternative or let me know what am I doing wrong? How could I extract only those rows with a different way?

I want to plot x= column 2 from row 4 to row 25 against y=column 2 from row 33 to row 54.

Thank you very much in advance.

Regards,

Gio

1
  • 2
    In the text you refer to wanting to plot certain rows, but you're using a different range of rows in the code: [4:24] vs. "from row 4 to row 25". Also, note that lists in Python start from 0, so [4:] is starting at the fifth item in the list. Commented Dec 6, 2012 at 16:39

2 Answers 2

3

What you are doing wrong is calling readlines two times.

A file object behaves like an iterator. Calling readlines will exhaust it. The second call will return an empty list.

You can get the list of lines once and then work with it:

lines = f.readlines()
lines1 = lines[4:24]
lines2 = lines[33:54]

Still, it looks like the lengths of the lists will differ by 1, I guess you need to correct that.

Also note that you don't need to convert lists to numpy arrays to plot them.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your help. Indeed, my lists differ by 1 from each other. Anyway it does work now.
1

You can solve this with numpy.genfromtxt and python slices:

import numpy as np
import matplotlib.pyplot as plt

x_start, x_end = 4, 25 # get values from the 4th to the 25rd line
y_start, y_end = 33, 54 # get values from the 33rd to the 54rd line

x = np.genfromtxt('datafile', usecols=(1))
y = np.genfromtxt('datafile', usecols=(1))

x = x[x_start - 1:x_end]
y = y[y_start - 1:y_end]

print ' x=', x, '\n\n y=', y

plt.plot(x, y)
plt.show()

5 Comments

Thank you very much for your help. Do you know any good tutorial to read for working on data with matplotlib and numpy?
However, it seems that np.genfromtxt reads only one column. How could I split my file? It says "got one columns instead of 1"
@gioR, native numpy and matplotlib documentation are pretty damn good. :) matplotlib.org/contents.html scipy.org/Tentative_NumPy_Tutorial Also I recommend you to use matplotlib gallery (matplotlib.org/gallery.html) if you need to solve some issue but doesn't know how to name it. And remember that google & stackoverflow is extremely powerful combination.
@gioR, np.genfromtxt reads here only one column (the second one with index 1) only because I set input param usecols=(1) at this example. Actually it's the function that reads whole file. Please, read the documentation here docs.scipy.org/doc/numpy/reference/generated/….
@gioR, ehm, sir, and one moment.. if your task solved by answers on this thread, please, can you mark one of them as 'accepted'? It will be +2 reputation for you and +15 reputation for answer's author. :) Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.