Python 3.x 悬停在图形上打印图形名称

Python 3.x 悬停在图形上打印图形名称,python-3.x,matplotlib,hover,Python 3.x,Matplotlib,Hover,我有3个具有特定名称的文本文件,我想在一个图形中绘制这些文件。以下是我的文件: 111.txt 0 0 1 1 2 2 3 3 222.txt 0 0 1 3 2 6 3 9 333.txt 0 0 1 5 2 10 3 15 当我在这些行上移动鼠标光标时,我希望图形的名称(不带“.txt”)显示在鼠标光标旁边。这就是我尝试过的: import matplotlib.pyplot as plt import os def GetFiles(): return [file for

我有3个具有特定名称的文本文件,我想在一个图形中绘制这些文件。以下是我的文件:

111.txt

0 0
1 1
2 2
3 3
222.txt

0 0
1 3
2 6
3 9
333.txt

0 0
1 5
2 10
3 15
当我在这些行上移动鼠标光标时,我希望图形的名称(不带“.txt”)显示在鼠标光标旁边。这就是我尝试过的:

import matplotlib.pyplot as plt
import os

def GetFiles():
    return [file for file in os.listdir('/home/myfolder') if 
file.endswith(".txt")]

#Get All Available file in current directry
textfiles=GetFiles()

#Storing the names of the files without .txt in an array
s=[]
for j in range (len(textfiles)):
    without_txt = textfiles[j].replace(".txt", "")
    s.append(without_txt)
#print (s)

fig = plt.figure()
plot = fig.add_subplot(111)

# plot the text files in the folder
for i in range(len(s)):
    plt.plotfile(str(textfiles[i]), delimiter=' ', cols=(0, 1), 
linestyle='-', linewidth=2, color='b', newfig=False,gid=s[i])

#plt.gca().invert_xaxis()

def on_plot_hover(event):
    for curve in plot.get_lines():
        if curve.contains(event)[0]:
            print "over %s" % curve.get_gid()  

fig.canvas.mpl_connect('motion_notify_event', on_plot_hover)
plt.show()

我遵循了文章中提到的第二种解决方案。它打印图形的名称,但正如我所说,我希望它在鼠标悬停时出现在鼠标光标旁边。我想不出来。任何帮助都将不胜感激。

您没有使用示例链接中的注释。只需将其添加到代码中,并将注释的
xy
值设置为悬停事件的
xdata
ydata
值,即可获得所需的内容:

import matplotlib.pyplot as plt
import os

def GetFiles():
    return [file for file in os.listdir('/home/myfolder') if 
file.endswith(".txt")]

#Get All Available file in current directry
textfiles=GetFiles()

#Storing the names of the files without .txt in an array
s=[]
for j in range (len(textfiles)):
    without_txt = textfiles[j].replace(".txt", "")
    s.append(without_txt)

fig = plt.figure()
plot = fig.add_subplot(111)

annot = plot.annotate("", xy=(2,2), xytext=(10,10),textcoords="offset points",
                    bbox=dict(boxstyle="round", fc="w"))
annot.set_visible(False)

# plot the text files in the folder
for i in range(len(s)):
    plt.plotfile(str(textfiles[i]), delimiter=' ', cols=(0, 1), 
linestyle='-', linewidth=2, color='b', newfig=False,gid=s[i])

def update_annot(x,y, text):
    annot.set_text(text)
    annot.xy = [x,y]

def on_plot_hover(event):
    vis = annot.get_visible()
    for curve in plot.get_lines():
        if curve.contains(event)[0]:
            print("over %s" % curve.get_gid())
            update_annot(event.xdata, event.ydata, curve.get_gid())
            annot.set_visible(True)
            fig.canvas.draw_idle()
        else:
            if vis:
                annot.set_visible(False)
                fig.canvas.draw_idle()


fig.canvas.mpl_connect('motion_notify_event', on_plot_hover)
plt.show()


链接的解决方案通过
plt.annotate
创建
注释。代码中完全没有这一部分。谢谢您的帮助。@Leo,我从中学到的一点是,您应该考虑使用与
plotfile
不同的绘图方式:“一旦您想对图形或轴进行重大更改,最好不要依赖
plotfile