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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:31:06+00:00 2026-06-12T19:31:06+00:00

what I am trying to do is having a script compute something, prepare a

  • 0

what I am trying to do is having a script compute something, prepare a plot and show the already obtained results as a pylab.figure – in python 2 (specifically python 2.7) with a stable matplotlib (which is 1.1.1).

In python 3 (python 3.2.3 with a matplotlib git build … version 1.2.x), this works fine. As a simple example (simulating a lengthy computation by time.sleep()) consider

import pylab
import time
import random

dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.draw()    
for i in range (18):
    dat.append(random.uniform(0,1))
    pylab.plot(dat)
    pylab.draw()
    time.sleep(1)

In python 2 (version 2.7.3 vith matplotlib 1.1.1), the code runs cleanly without errors but does not show the figure. A little trial and error with the python2 interpreter seemed to suggest to replace pylab.draw() with pylab.show(); doing this once is apparently sufficient (not, as with draw calling it after every change/addition to the plot). Hence:

import pylab
import time
import random

dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.show()    
for i in range (18):
    dat.append(random.uniform(0,1))
    pylab.plot(dat)
    #pylab.draw()
    time.sleep(1)

However, this doesn’t work either. Again, it runs cleanly but does not show the figure. It seems to do so only when waiting for user input. It is not clear to me why this is, but the plot is finally shown when a raw_input() is added to the loop

import pylab
import time
import random

dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.show()    
for i in range (18):
    dat.append(random.uniform(0,1))
    pylab.plot(dat)
    #pylab.draw()
    time.sleep(1)
    raw_input()

With this, the script will of course wait for user input while showing the plot and will not continue computing the data before the user hits enter. This was, of course, not the intention.

This may be caused by different versions of matplotlib (1.1.1 and 1.2.x) or by different python versions (2.7.3 and 3.2.3).

Is there any way to accomplish with python 2 with a stable (1.1.1) matplotlib what the above script (the first one) does in python 3, matplotlib 1.2.x:
– computing data (which takes a while, in the above example simulated by time.sleep()) in a loop or iterated function and
– (while still computing) showing what has already been computed in previous iterations
– and not bothering the user to continually hit enter for the computation to continue

Thanks; I’d appreciate any help…

  • 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-12T19:31:07+00:00Added an answer on June 12, 2026 at 7:31 pm

    You want the pause function to give the gui framework a chance to re-draw the screen:

    import pylab
    import time
    import random
    import matplotlib.pyplot as plt
    
    dat=[0,1]
    fig = plt.figure()
    ax = fig.add_subplot(111)
    Ln, = ax.plot(dat)
    ax.set_xlim([0,20])
    plt.ion()
    plt.show()    
    for i in range (18):
        dat.append(random.uniform(0,1))
        Ln.set_ydata(dat)
        Ln.set_xdata(range(len(dat)))
        plt.pause(1)
    
        print 'done with loop'
    

    You don’t need to create a new Line2D object every pass through, you can just update the data in the existing one.

    Documentation:

    pause(interval)
        Pause for *interval* seconds.
    
        If there is an active figure it will be updated and displayed,
        and the gui event loop will run during the pause.
    
        If there is no active figure, or if a non-interactive backend
        is in use, this executes time.sleep(interval).
    
        This can be used for crude animation. For more complex
        animation, see :mod:`matplotlib.animation`.
    
        This function is experimental; its behavior may be changed
        or extended in a future release.
    

    A really over-kill method to is to use the matplotlib.animate module. On the flip side, it gives you a nice way to save the data if you want (ripped from my answer to Python- 1 second plots continous presentation).

    example, api, tutorial

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

Sidebar

Related Questions

I'm having terrible trouble trying to understand python scoping rules. With the following script:
I am having difficulty trying to figure out a bug in my Python (2.7)
I'm trying to run a script from the commandline, and python is having absolutely
Having problems with a small awk script, Im trying to choose the newest of
I've been having real problems trying to get a ruby script to run through
I'm trying to translate a Perl script to PHP and I'm having trouble with
I'm trying to debug a cons script, and the problem I'm having is that
I am having trouble trying to get this script to work. When I debug
Im having trouble trying to find out why my script wont delete a row
Im having problems trying to empty a folder in my script. This is working

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.