matplotlib animated plot wont update labels on axis using blit

风格不统一 提交于 2019-11-30 15:00:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/10368371/matplotlib-animated-plot-wont-update-labels-on-axis-using-blit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!