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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:30:27+00:00 2026-06-10T15:30:27+00:00

I recently started tinkering with python and the matplotlib module(1.1.0) shipped with Enthought Python

  • 0

I recently started tinkering with python and the matplotlib module(1.1.0) shipped with Enthought Python Distribution(the free version one).
I thought of an interesting project I could do and came up with something like this:

  • get ping of an internet address
  • pipe that via sys.stdin into python script

now in the python script:

  • regex the answer time, if there is no answer time: use NaN or just 0 as number
  • plot data via matplotlib
  • add continuously data

I managed to get the ping via this: ping stackoverflow.com | python script.py
get the answertime wasn’t particulary hard. But when it comes to plotting the data. I am stuck. I know there is an animation module inside matplotlib, but I think a timer-based plot would be harder to program than this, and I don’t know how events are used anyway. What I want:

  • Wait for sys.stdin to get a new string and thus the ping time
  • Add it to the data array
  • Plot the data array
    But it doesn’t seem to be that easy. Besides this, Error handling is not yet done..
    Unfortunately I couldn’t find any comparable code, although I did a lot of googling about it :/ Maybe this design is not meant to be like this..

Does anybody have an idea how to accomplish this replotting? It doesn’t need to be efficient, as the ping only comes in every second or so..
I thought about buffering the incoming stdin and do a regular timer-based plot, but I have now idea to do that.

Thank you in advance,

Jonas

Update1:

I can get rid of this error by using:

l1.set_xdata(range(len(data)))

before the l1.set_ydata(..), but still it doesn’t plot anything and the window doesn’t respond either. At least it shows the plot axis.

Code

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


def main():
    if sys.stdin.isatty():
        print "Please use a pipe as stdin\nExample: ping stackoverflow.com | python script.py"
        return 0
    regex = re.compile('time=(\d+.\d+)')
    data = []
    fig = plt.figure()

    ax = fig.add_subplot(111)
    ax.set_yscale('linear') # or log
    ax.grid(True)

    l1, = ax.plot(data)
    fig.show()

    while True:
        #get ping, if no string: stream has ended
        line = sys.stdin.readline()
        if line == '':
            break
        #conversion: 64 bytes from 127.0.0.1: icmp_seq=0 ttl=45 time=100.873 ms --> 100.873
        match = regex.findall(line)
        number = 0.

        if len(match) > 1:
            raise ValueError()
        if len(match) == 1:
            try:
                number = float(match[0])
            except ValueError as e:
                print e
        #add number to array, plot the data
        data.append(number)
        l1.set_xdata(range(len(data)))
        l1.set_ydata(data)

        ax.relim()
        ax.autoscale()

        plt.draw()
        fig.canvas.flush_events()
    return 0

if __name__ == '__main__':
    sys.exit(main())
  • 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-10T15:30:29+00:00Added an answer on June 10, 2026 at 3:30 pm

    Where you first have plt.show(), do not use that. Instead use fig.show() there. And then just plt.draw() in your loop. The rest seems good. The thing is that plt.show() starts a mainloop, locking further execution (outside the events for the figure). fig.show() seems not to, however the buttons probably will not work like this (I didn’t try).

    You will have to add autoscale though:

    ax.relim()
    ax.autoscale()
    plt.draw()
    # And this allows you to at least close the window (and crash the program by that ;))
    fig.canvas.flush_events()
    

    But really it should be cleaner not use pyplot for this. For that look into how to embed matplotlib into a gui, it is really easy and can be done for a couple of different gui toolkits.

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

Sidebar

Related Questions

I recently started a project in Symfony2 and I need to run some methods
I've recently started tinkering with ASP.NET MVC, but this question should apply to classic
I recently started learning 0MQ . Earlier today, I ran into a blog, Python
Recently started on python, wondered what the equivalent object was for storing session &
I recently started a project where I have to authenticate users against the Active
I recently started a project where the team decided we'd like to use jQuery
I started tinkering around with HTML5 recently and am very interested in Audio tag.
I recently started playing around with writing Perl (v5.8.8) extensions using XS. One of
I recently started using WPF and the MVVM framework, one thing that I have
I recently started tracking changes for my project with Mercurial. I made several revisions

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.