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

  • Home
  • SEARCH
  • 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 8933511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:33:27+00:00 2026-06-15T09:33:27+00:00

I am creating a graph in python using a text file for the source

  • 0

I am creating a graph in python using a text file for the source data and matplotlib to plot the graph.
The simple logic below works well.

But is there a way to get have numpy.gentfromtxt only read the first 50 lines from the file ‘temperature_logging’? Currently it reads the entire file.

temp = numpy.genfromtxt('temperature_logging',dtype=None,usecols=(0))
time = numpy.genfromtxt('temperature_logging',dtype=None,usecols=(1))

dates = matplotlib.dates.datestr2num(time)

pylab.plot_date(dates,temp,xdate=True,fmt='b-')

pylab.savefig('gp.png')

contents in temperature_logging;

21.75 12-01-2012-15:53:35    
21.75 12-01-2012-15:54:35    
21.75 12-01-2012-15:55:35    
.
.
.
  • 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-15T09:33:27+00:00Added an answer on June 15, 2026 at 9:33 am

    numpy.genfromtxt accepts iterators as well as files. That means it will accept the output of itertools.islice. Here, test.txt is a five-line file:

    >>> import itertools, numpy
    >>> with open('test.txt') as t_in:
    ...     numpy.genfromtxt(itertools.islice(t_in, 3))
    ... 
    array([[  1.,   2.,   3.,   4.,   5.],
           [  6.,   7.,   8.,   9.,  10.],
           [ 11.,  12.,  13.,  14.,  15.]])
    

    One might think this would be slower than letting numpy handle the file IO, but a quick test suggests otherwise. genfromtxt provides a skip_footer keyword argument that you can use if you know how long the file is…

    >>> numpy.genfromtxt('test.txt', skip_footer=2)
    array([[  1.,   2.,   3.,   4.,   5.],
           [  6.,   7.,   8.,   9.,  10.],
           [ 11.,  12.,  13.,  14.,  15.]])
    

    …but a few informal tests on a 1000-line file suggest that using islice is faster even if you skip only a few lines:

    >>> def get(nlines, islice=itertools.islice):
    ...     with open('test.txt') as t_in:
    ...         numpy.genfromtxt(islice(t_in, nlines))
    ...         
    >>> %timeit get(3)
    1000 loops, best of 3: 338 us per loop
    >>> %timeit numpy.genfromtxt('test.txt', skip_footer=997)
    100 loops, best of 3: 4.92 ms per loop
    >>> %timeit get(300)
    100 loops, best of 3: 5.04 ms per loop
    >>> %timeit numpy.genfromtxt('test.txt', skip_footer=700)
    100 loops, best of 3: 8.48 ms per loop
    >>> %timeit get(999)
    100 loops, best of 3: 16.2 ms per loop
    >>> %timeit numpy.genfromtxt('test.txt', skip_footer=1)
    100 loops, best of 3: 16.7 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a data model using xcode and core data. It's an object graph,
im currently creating a graph for an app, using coreplot and have a problem
I am creating graphs using GD::Graph::lines . For option set, I have to write
I am creating a popup window using tkinter's Toplevel containing a pyplot graph, and
Hi I just define my custom action on facebook using open graph. After creating
Long story short, I'm creating a graph using networkx and the lower the weights
I'm creating a DOT graph visualization from a tree-like data structure but am having
I'm creating a REST service in Grails to accept data from a python script.
I'm trying to convert the data from a simple object graph into a dictionary.
I'm creating a graph using Graphviz (compiled with neato). This graph contains many overlapping

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.