【问题标题】:Matplotlib blitting does not update my canvas [duplicate]Matplotlib blitting 不会更新我的画布 [重复]
【发布时间】:2016-12-05 17:07:18
【问题描述】:

我有一个使用 matplotlib 画布的应用程序,该画布基本上包含一个 imshow 和一些艺术家对象(例如椭圆)。图形画布绑定到以下事件序列:

  • 右键选择艺术家对象 --> 将改变艺术家的面部颜色
  • 左键释放 --> 将选定的艺术家移动到新位置

为了加快绘图速度,我必须使用 blitting。当我运行事件序列时,选择要移动的椭圆同时显示在画布中的旧坐标和新坐标中。当我将 blitting 机器替换为 canvas.draw() 时,不会出现此问题。

你知道我在使用 blitting 时做错了什么吗?

这是一个快速而肮脏的 sn-p,它重现了我的问题(ubuntu 12.04、python 2.7、matplotlib 1.1.1rc)。

import numpy
from pylab import figure, show
from matplotlib.patches import Ellipse

def on_pick_ellipse(event):

    if event.mouseevent.button == 3:
        ellipse = event.artist
        ellipse.set_facecolor((1,0,0))
        subplot.draw_artist(ellipse)
        fig.canvas.blit(subplot.bbox)

    return True

def on_move_ellipse(event):

    global ellipse

    if event.button == 3:
        return 

    if ellipse is not None :
        fig.canvas.restore_region(background)
        newCenter = (event.xdata, event.ydata)
        ellipse.center = newCenter
        ellipse.set_facecolor((0,0,1))
        subplot.draw_artist(ellipse)
        fig.canvas.blit(subplot.bbox)
        ellipse = None
        return True

ellipse = None

data = numpy.random.uniform(0,1,(640,256))

fig = figure()
subplot = fig.add_subplot(111,aspect="equal")
subplot.imshow(data.T)

background = fig.canvas.copy_from_bbox(subplot.bbox)

ellipse = Ellipse(xy=(100,100), width=100, height=30, angle=30.0, picker=True)
ellipse.set_clip_box(subplot.bbox)
ellipse.set_alpha(0.7)
ellipse.set_facecolor((0,0,1))

subplot.add_artist(ellipse)

fig.canvas.mpl_connect("pick_event", on_pick_ellipse)
fig.canvas.mpl_connect("button_release_event", on_move_ellipse)

show()

非常感谢

埃里克

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    在调用fig.canvas.copy_from_bbox之前必须绘制一次画布。

    所以上面的代码可以正常工作。

    fig.canvas.draw()
    background = fig.canvas.copy_from_bbox(subplot.bbox)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多