鼠标悬停在散布图上的数据点上,显示实验室

2024-10-01 13:34:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集,形式如下:

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

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


Tags: 数据名称datamatplotlib绘制鼠标形式指针
1条回答
网友
1楼 · 发布于 2024-10-01 13:34:08

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

我希望它有用

相关问题 更多 >