如何在matplotlib中仅绘制注释而不是整个画布?

时间:2016-01-10 03:48:57

标签: python matplotlib

event.canvas.draw()似乎在下面的代码中需要很长时间,我怎样才能绘制注释而不是整个画布?

import random
import matplotlib.pyplot as plt

def on_move(event):
    if event.inaxes is not None:
        x = event.xdata
        y = event.ydata
        annotation.xy = (x, y)
        annotation.set_text("Test")
        event.canvas.draw()

fig = plt.figure()
ax = plt.gca()
ax.plot(random.sample(xrange(100000), 50000), random.sample(xrange(100000), 50000), 'bo')
ax.plot(random.sample(xrange(100000), 50000), random.sample(xrange(100000), 50000), 'go')

annotation = ax.annotate(
    '', 
    xy=(0,0), 
    xycoords='data',
    ha = 'right',
    xytext = (-20, 20), 
    textcoords = 'offset points', 
    va = 'bottom',
    bbox = dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.75),
    arrowprops = dict(
                      arrowstyle='->', 
                      connectionstyle='arc3,rad=0', 
                      relpos=(1., 0.))
    )
fig.canvas.mpl_connect('motion_notify_event', on_move)

plt.show()

0 个答案:

没有答案