在鼠标悬停在散点图matplotlib图上的数据点上,显示标签

时间:2013-09-24 00:08:11

标签: python matplotlib

我的数据集形式为:

data = [('name1','value1','factor1')...[('nameN','valueN','factorN')]

我试图将值绘制为y(x只是数据中y的索引)。我希望matPlotLib打印鼠标指针悬停的值的相应名称和因子。 我找到了一个实现的例子,但它使用了wxPython,不幸的是我无法使用它。任何方式(包括名称可以显示在matplotlib的工具栏中,或者只是作为标签出现在点上并在移开指针时消失)将非常有帮助。

1 个答案:

答案 0 :(得分:0)

https://mpld3.github.io/examples/scatter_tooltip.html

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()

我希望它有用