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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:11:41+00:00 2026-06-09T17:11:41+00:00

Objective: To plot a graph(x,y) and move a vertical line over the graph w.r.t

  • 0

Objective: To plot a graph(x,y) and move a vertical line over the graph w.r.t to timer.

I started implementing this using matplotlib. Its possible to implement this using draw() feature of matplotlib , but it consumes cpu as it redraws every time and doesn’t allow me to interact with the graph. so i decided to use animation feature of matplotlib. in Future i would aslo like to pause the moving line. So i cant use matplotlib.animation.FuncAnimatin

Problem: I use canvas.copy_from_bbox(ax.bbox), ax.draw_artist(line),canvas.blit(ax.bbox).
But, I am not able to store the graph in the background and move a line over it. When i try to store, it overwrites in a quite weird way.

This is the code i have built. Could any one please help me ? Thanks in advance.

import sys
import matplotlib.pyplot as p
import time
fig=p.figure();
ax = fig.add_subplot(1,1,1)

y=[];x=[];y1=[0,1000];x1=[0,0]
y=numpy.random.randn(1000,1)*100
x=numpy.arange(0,1000)
line1, = ax.plot(x,y,color='black');
ax.set_ylim(0, 1000);
line, = ax.plot(x1,y1,color='r',alpha=1,animated=True); # this is the line which i wanted to move over the graph w.r.t to time. ( i can also use axvline , but i guess its the same).
canvas = ax.figure.canvas
canvas.draw()
background = canvas.copy_from_bbox(ax.bbox); #my problem is here
starttime=time.time();
mytimer=0;
mytimer_ref=0;
def update(canvas,line,ax):
    canvas.restore_region(background) #my problem is here 
    t=time.time()-starttime;
    mytimer=t+mytimer_ref;
    x1=[mytimer,mytimer];
    line.set_xdata(x1);
    ax.draw_artist(line)
    canvas.blit(ax.bbox) #my problem is here

def onclick(event):
    global starttime
    starttime=time.time();
    global mytimer_ref;
    mytimer_ref=event.xdata;
    print "starttime",starttime;


cid=line1.figure.canvas.mpl_connect('button_press_event',onclick); # when i click the mouse over a point, line goes to that point and start moving from there. 
timer=fig.canvas.new_timer(interval=100);
args=[canvas,line,ax];
timer.add_callback(update,*args); # every 100ms it calls update function
timer.start();
p.show();
  • 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-09T17:11:42+00:00Added an answer on June 9, 2026 at 5:11 pm

    So it looks like the “quite weird way” you are referring to is essentially that the wrong bbox has been captured with your background = canvas.copy_from_bbox(ax.bbox). I believe this is a known problem with most of the backends where the addition of toolbars etc. affect the position of the bbox for blitting.

    Essentially, if you can capture the background after the window has popped up, then everything should be working for you. This can be done in a number of ways, in your case the simplest would be to replace your canvas.draw() command with a plt.show(block=False), which will bring up the window, without making it a blocking command.

    As a slight addition, I’m sure you are aware that semicolons are not necessary in python code, but while I was debugging, I tidied up your code a little (didn’t quite get to the end):

    import sys
    import matplotlib.pyplot as plt
    import time
    import numpy
    
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    
    max_height = 100
    n_pts = 100
    y1 = [0, max_height]
    x1 = [0, 0]
    y = numpy.random.randn(n_pts) * max_height
    x = numpy.arange(0, n_pts)
    
    # draw the data
    line1, = ax.plot(x, y, color='black')
    
    # fix the limits of the plot
    ax.set_ylim(0, max_height)
    ax.set_xlim(0, n_pts)
    
    # draw the plot so that we can capture the background and then use blitting
    plt.show(block=False)
    
    # get the canvas object
    canvas = ax.figure.canvas
    background = canvas.copy_from_bbox(ax.bbox)
    
    # add the progress line.
    # XXX consider using axvline
    line, = ax.plot(x1, y1, color='r', animated=True) 
    
    
    starttime=time.time()
    mytimer=0
    mytimer_ref=0
    
    def update(canvas, line, ax):
        # revert the canvas to the state before any progress line was drawn
        canvas.restore_region(background)
    
        # compute the distance that the progress line has made (based on running time) 
        t = time.time() - starttime
        mytimer = t + mytimer_ref
        x1 = [mytimer,mytimer]
        # update the progress line with its new position
        line.set_xdata(x1)
        # draw the line, and blit the axes
        ax.draw_artist(line)
        canvas.blit(ax.bbox)
    
    def onclick(event):
        global starttime
        starttime=time.time()
        global mytimer_ref
        mytimer_ref=event.xdata
        print "starttime",starttime
    
    
    cid=line1.figure.canvas.mpl_connect('button_press_event',onclick) # when i click the mouse over a point, line goes to that point and start moving from there. 
    timer=fig.canvas.new_timer(interval=100)
    args=[canvas,line,ax]
    timer.add_callback(update,*args) # every 100ms it calls update function
    timer.start()
    plt.show()
    

    HTH

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

Sidebar

Related Questions

Core plot objective-c, I am using CPTAxisLabelingPolicyAutomatic. my minorTickLabels are over writing the majorTickLabel
(Objective C) Just using simple AudioServicesPlaySystemSoundID and its counterparts, but I can't find in
Using Objective-C, how can I give/take the permissions for all user to read a
In objective-c whats the difference between using a . and using ->? I've used
Objective-C Answers are fine, I am using MonoTouch for reference. I am writing text
( Objective C ) How would I call a base class function using a
In Objective-C I have a timer fire every 0.1 seconds and increment a double value
My Objective-C is pretty rusty or non-existent. I'm trying to figure out how this
Objective C answers are fine too. This is in C# Monotouch. Currently I am
Objective-J is compiled/transformed into JavaScript directly on the browser. (This is contrast to doing

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.