Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:19:30+00:00 2026-06-15T13:19:30+00:00

I have a data file where I want to plot specific lines of the

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:19:31+00:00Added an answer on June 15, 2026 at 1:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data file which I want to plot with gnuplot. However, for
Say I have a data file that I want to process; I want to
I have an xml data file and I want to populate a listview in
I have a number of images and a CSV data file that I want
I have hard-coded data into a .jsp file. What I want to do is
I have Uitable with data read from a AScii file. I want to select
I have a file, where I want to copy some data of the file
I have third-party C code that writes data to a file. I want to
I have one problem , I want to get some data from XML file
i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.