Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib animated plot wont update labels on axis using blit

I am plotting data in a plot using wxPython where the data limits on the y- axis are changing with the data. I would like to change the axis dynamically without redrawing the whole canvas like canvas.draw() rather I'd like to use blitting for this as I do for the plot itself.

What I got to work is the changing y-axis, and I get the yticks animated with the plot, unfortunately the ylabels are gone and I cant find the solution. The reason is setting the get_yaxis().set_animated(True) setting for the axis.

I put together a little working example in the following. What am I missing here?

import matplotlib
matplotlib.use('WXAgg')

import wx
import pylab as p
import numpy as npy
from time import sleep

ax = p.subplot(111)
canvas = ax.figure.canvas
x = npy.arange(0,2*npy.pi,0.01)
line, = p.plot(x, npy.sin(x), animated=True)

ax.get_yaxis().set_animated(True)

def update_line(*args):
    if update_line.background is None:
        update_line.background = canvas.copy_from_bbox(ax.bbox)

    for i in range(20):
        canvas.restore_region(update_line.background)

        line.set_ydata((i/10.0)*npy.sin(x))
        ax.set_ylim(-1*i/5.0-0.5,i/5.0+0.5)

        ax.draw_artist(ax.get_yaxis())

        ax.draw_artist(line)

        canvas.blit(ax.bbox)

        sleep(0.1)
    print 'end'


update_line.cnt = 0
update_line.background = None
wx.EVT_IDLE(wx.GetApp(), update_line)
p.show()

Basically I am looking for something like get_ylabels().set_animated(True) but I cant find it.

like image 583
Merlin Avatar asked Apr 28 '12 23:04

Merlin


1 Answers

It looks like the labels are drawn but the blit command doesn't copy them over to the canvas because the bounding box only includes the inner part of the axes.

For me changing update_line.background = canvas.copy_from_bbox(ax.bbox) to update_line.background = canvas.copy_from_bbox(ax.get_figure().bbox) and canvas.blit(ax.bbox) to canvas.blit(ax.clipbox) made it work.

like image 98
Henning Avatar answered Sep 21 '22 06:09

Henning