hover在matplotlib中的一个点上时可能会出现标签?

我正在使用matplotlib来制作散点图。 散点图上的每个点都与一个命名对象相关联。 当我将光标hover在与该对象相关联的散点图上的点上时,我希望能够看到对象的名称。 特别是,能够快速查看exception点的名称是很好的。 我在这里search的时候能够find的最接近的就是注释命令,但是这似乎在图上创build了一个固定的标签。 不幸的是,用我有的点数,散点图将是不可读的,如果我标记每个点。 有没有人知道创build标签的方法,只有当光标hover在该点附近时才会出现?

这里似乎没有其他答案实际上回答了这个问题。 所以这是一个使用散点的代码,并在hover点上显示注释

import matplotlib.pyplot as plt import numpy as np; np.random.seed(1) x = np.random.rand(15) y = np.random.rand(15) names = np.array(list("ABCDEFGHIJKLMNO")) c = np.random.randint(1,5,size=15) norm = plt.Normalize(1,4) cmap = plt.cm.RdYlGn fig,ax = plt.subplots() sc = plt.scatter(x,y,c=c, s=100, cmap=cmap, norm=norm) annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points", bbox=dict(boxstyle="round", fc="w"), arrowprops=dict(arrowstyle="->")) annot.set_visible(False) def update_annot(ind): pos = sc.get_offsets()[ind["ind"][0]] annot.xy = pos text = "{}, {}".format(" ".join(list(map(str,ind["ind"]))), " ".join([names[n] for n in ind["ind"]])) annot.set_text(text) annot.get_bbox_patch().set_facecolor(cmap(norm(c[ind["ind"][0]]))) annot.get_bbox_patch().set_alpha(0.4) def hover(event): vis = annot.get_visible() if event.inaxes == ax: cont, ind = sc.contains(event) if cont: update_annot(ind) 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", hover) plt.show() 

在这里输入图像描述

我知道这是一个古老的问题,但我一直在寻找一个解决schemehover(不点击)一行到达这里。

 import matplotlib.pyplot as plt fig = plt.figure() plot = fig.add_subplot(111) # create some curves for i in range(4): plot.plot( [i*1,i*2,i*3,i*4], gid=i) 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() 

http://matplotlib.sourceforge.net/examples/event_handling/pick_event_demo.html

 from matplotlib.pyplot import figure, show import numpy as npy from numpy.random import rand if 1: # picking on a scatter plot (matplotlib.collections.RegularPolyCollection) x, y, c, s = rand(4, 100) def onpick3(event): ind = event.ind print 'onpick3 scatter:', ind, npy.take(x, ind), npy.take(y, ind) fig = figure() ax1 = fig.add_subplot(111) col = ax1.scatter(x, y, 100*s, c, picker=True) #fig.savefig('pscoll.eps') fig.canvas.mpl_connect('pick_event', onpick3) show() 
  • 这个配方在选取一个数据点时会绘制一个注释: http : //scipy-cookbook.readthedocs.io/items/Matplotlib_Interactive_Plotting.html 。
  • 这个配方画一个工具提示,但它需要wxPython: matplotlib中的点和线工具提示?

http://matplotlib.org/users/shell.html中提供的示例稍作修改:;

 import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.set_title('click on points') line, = ax.plot(np.random.rand(100), '-', picker=5) # 5 points tolerance def onpick(event): thisline = event.artist xdata = thisline.get_xdata() ydata = thisline.get_ydata() ind = event.ind print 'onpick points:', zip(xdata[ind], ydata[ind]) fig.canvas.mpl_connect('pick_event', onpick) plt.show() 

正如Sohaib所问,这是一条直线阴谋

mpld3为我解决。 编辑(添加代码):

 import matplotlib.pyplot as plt import numpy as np import mpld3 fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE')) N = 100 scatter = ax.scatter(np.random.normal(size=N), np.random.normal(size=N), c=np.random.random(size=N), s=1000 * np.random.random(size=N), alpha=0.3, cmap=plt.cm.jet) ax.grid(color='white', linestyle='solid') ax.set_title("Scatter Plot (with tooltips!)", size=20) labels = ['point {0}'.format(i + 1) for i in range(N)] tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels) mpld3.plugins.connect(fig, tooltip) mpld3.show() 

你可以检查这个例子