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 6654035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:30+00:00 2026-05-26T01:20:30+00:00

I recently decided to give matplotlib.pyplot a try, while having used gnuplot for scientific

  • 0

I recently decided to give matplotlib.pyplot a try, while having used gnuplot for scientific data plotting for years. I started out with simply reading a data file and plot two columns, like gnuplot would do with plot 'datafile' u 1:2.
The requirements for my comfort are:

  • Skip lines beginning with a # and skip empty lines.
  • Allow arbitrary numbers of spaces between and before the actual numbers
  • allow arbitrary numbers of columns
  • be fast

Now, the following code is my solution for the problem. However, compared to gnuplot, it really is not as fast. This is a bit odd, since I read that one big advantage of py(plot/thon) over gnuplot is it’s speed.

import numpy as np
import matplotlib.pyplot as plt
import sys

datafile = sys.argv[1]
data = []
for line in open(datafile,'r'):
    if line and line[0] != '#':
        cols = filter(lambda x: x!='',line.split(' '))
        for index,col in enumerate(cols):
            if len(data) <= index:
                data.append([])
            data[index].append(float(col))

plt.plot(data[0],data[1])
plt.show()

What would I do to make the data reading faster? I had a quick look at the csv module, but it didn’t seem to be very flexible with comments in files and one still needs to iterate over all lines in the file.

  • 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-05-26T01:20:30+00:00Added an answer on May 26, 2026 at 1:20 am

    Since you have matplotlib installed, you must also have numpy installed. numpy.genfromtxt meets all your requirements and should be much faster than parsing the file yourself in a Python loop:

    import numpy as np
    import matplotlib.pyplot as plt
    
    import textwrap
    fname='/tmp/tmp.dat'
    with open(fname,'w') as f:
        f.write(textwrap.dedent('''\
            id col1 col2 col3
            2010 1 2 3 4
            # Foo
    
            2011 5 6 7 8
            # Bar        
            # Baz
            2012 8 7 6 5
            '''))
    
    data = np.genfromtxt(fname, 
                         comments='#',    # skip comment lines
                         dtype = None,    # guess dtype of each column
                         names=True)      # use first line as column names
    print(data)
    plt.plot(data['id'],data['col2'])
    plt.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started a Rails project recently and decided to use RESTful controllers. I created
I recently started a new webforms project and decided to separate the business classes
I've been interested in D for a couple of years now and recently decided
I have recently started a project in Ruby on Rails. I used to do
I recently decided to try to learn some bash scripting and as a fun
I come from a Java background and recently decided to try creating a C++
I recently decided that I wanted to try learning how to program in assembly.
I've recently decided that I just have to finally learn C/C++, and there is
This is starting to vex me. I recently decided to clear out my FTP,
I'm a beginner (self-learning) programmer learning C++, and recently I decided to implement a

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.