Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
matplotlib,使用可拖动图例移动注释_Matplotlib_Annotations_Draggable_Legend - Fatal编程技术网

matplotlib,使用可拖动图例移动注释

matplotlib,使用可拖动图例移动注释,matplotlib,annotations,draggable,legend,Matplotlib,Annotations,Draggable,Legend,我试图在传递图例文本时对图例进行注释。这可以工作,但会重复注释,而且当图例移动(leg.draggable(state=True))时,注释也会重复,我无法删除它 下面是重现问题的简化代码: import numpy as np import matplotlib.pyplot as plt #define functions t = np.arange(0.0, 0.2, 0.1) y1 = 2*np.sin(2*np.pi*t) y2 = 4*np.sin(2*np.pi*2*t) #d

我试图在传递图例文本时对图例进行注释。这可以工作,但会重复注释,而且当图例移动(leg.draggable(state=True))时,注释也会重复,我无法删除它

下面是重现问题的简化代码:

import numpy as np
import matplotlib.pyplot as plt

#define functions
t = np.arange(0.0, 0.2, 0.1)
y1 = 2*np.sin(2*np.pi*t)
y2 = 4*np.sin(2*np.pi*2*t)

#define graphs
fig, ax = plt.subplots()
line0, = ax.plot(t, y1, label='line0')
line1, = ax.plot(t, y2, label='line1')
leg = ax.legend(loc=2)
leg.draggable(state=True) #enable dragging legend

######################test mouse passing legend text#############
fig.canvas.draw()#draw first to get legend position

legtext = leg.get_texts()
line0 = legtext[0] #text of legend 0
line1 = legtext[1] #text of legend 1

def on_move(event):
    annotations = []

    if line0.contains(event)[0] == True:
        p = leg.get_window_extent()
        annotations.append(ax.annotate('Annotation Text 0', (p.p0[0], p.p1[1]), xycoords='figure pixels', zorder=9))
        # print 'line0', annotations
        fig.canvas.draw()
    elif line1.contains(event)[0] == True:
        p = leg.get_window_extent()
        annotations.append(ax.annotate('Annotation Text 1', (p.p0[0], p.p1[1]), xycoords='figure pixels', zorder=9))
        # print 'line1', annotations
        fig.canvas.draw()
    else:
        # print 'else', annotations
        for note in annotations:
            annotations[note].remove()
            # print 'annotation removed', annotations[note]
            fig.canvas.draw()
fig.canvas.mpl_connect('motion_notify_event', on_move)

plt.show()

当鼠标不在图例上方时,有人能帮我删除注释吗?已经谢谢您了。

您正在创建一个名为
注释的列表,添加或删除其中的元素。实际上,您要做的是修改
ax.text
,这是一个包含所有ax注释的列表

def on_move(event):

    if line0.contains(event)[0] == True:
        p = leg.get_window_extent()
        ax.annotate('Annotation Text 0', (p.p0[0], p.p1[1]), xycoords='figure pixels', zorder=9)

    elif line1.contains(event)[0] == True:
        p = leg.get_window_extent()
        ax.annotate('Annotation Text 1', (p.p0[0], p.p1[1]), xycoords='figure pixels', zorder=9)

    else:
        ax.texts = []

    fig.canvas.draw()
如果从左侧抓取图例框,效果很好;如果从“line0”或“line1”抓取,则在移动图例时可能会看到注释出现或消失。希望没问题。一旦停止移动图例框,它也会停止