当鼠标移到d上时如何显示数据标签

2024-09-24 04:24:17 发布

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

我正在绘制一些看起来像

931,Oxfordshire,9314125,123255,Larkmead School,Abingdon,125,124,20,SUPP,8
931,Oxfordshire,9314126,123256,John Mason School,Abingdon,164,164,25,6,16
931,Oxfordshire,9314127,123257,Fitzharrys School,Abingdon,150,149,9,0,11
931,Oxfordshire,9316076,123298,Our Lady's Abingdon,Abingdon,57,57,SUPP,SUPP,16

我的基本步骤是

^{pr2}$

不过,当我把鼠标移到图表上时,我真的很想知道每个学校的名称,这样我就可以浏览数据了。有办法吗?在


Tags: 图表绘制步骤our鼠标johnschoolmason
1条回答
网友
1楼 · 发布于 2024-09-24 04:24:17

为了插入我自己的项目,看看mpldatacursorhttps://github.com/joferkington/mpldatacursor

作为一个基本的例子,只要调用datacursor(hover=True, point_labels=df['E'])就可以让你90%的时间得到你想要的东西。例如,以上面的代码片段为例:

from StringIO import StringIO
import pandas as pd
import matplotlib.pyplot as plt
from mpldatacursor import datacursor

f = StringIO(
"""931,Oxfordshire,9314125,123255,Larkmead School,Abingdon,125,124,20,SUPP,8
931,Oxfordshire,9314126,123256,John Mason School,Abingdon,164,164,25,6,16
931,Oxfordshire,9314127,123257,Fitzharrys School,Abingdon,150,149,9,0,11
931,Oxfordshire,9316076,123298,Our Lady's Abingdon,Abingdon,57,57,SUPP,SUPP,16
""")
df = pd.read_csv(f, names=['A','B','C','D','E','F','G', 'H','I','J', 'K'],
                 header=None)
df.replace('SUPP', 3.0, inplace=True)
df = df.convert_objects(convert_numeric=True)
df['KG'] = df['K']*1.0/df['G']
plt.plot(df['KG'], marker='o')

datacursor(hover=True, point_labels=df['E'])

plt.show()

enter image description here

当行悬停在上面时,我们会得到一个弹出标签。在

但是,根据设计,默认行为是在鼠标悬停/单击该行时显示弹出窗口。因此,当使用point_labels选项时,结果可能不是您所想的那样:

enter image description here

如果只希望在顶点悬停时显示弹出窗口,则可以使用类似的解决方法:(在下一版本中,将有一个仅在顶点处显示弹出窗口的选项,因此将来不再需要此解决方法。)

^{pr2}$

此外,您可能希望只显示相关学校,而不是x、y坐标等。要更改此设置,请使用自定义的formatter函数:

datacursor(l, hover=True, point_labels=df['E'],
           formatter=lambda **kwargs: kwargs['point_label'][0])

enter image description here

最后,你可能需要一个白色的盒子,上面有一个漂亮的箭头和一个不同的相对位置:

datacursor(l, hover=True, point_labels=df['E'], bbox=dict(fc='white'),
           formatter=lambda **kwargs: kwargs['point_label'][0], xytext=(0, 25),
           arrowprops=dict(arrowstyle='simple', fc='white', alpha=0.5))

enter image description here

最后一个例子就是把它们放在一个可运行的版本中:

from StringIO import StringIO
import pandas as pd
import matplotlib.pyplot as plt
from mpldatacursor import datacursor

f = StringIO(
"""931,Oxfordshire,9314125,123255,Larkmead School,Abingdon,125,124,20,SUPP,8
931,Oxfordshire,9314126,123256,John Mason School,Abingdon,164,164,25,6,16
931,Oxfordshire,9314127,123257,Fitzharrys School,Abingdon,150,149,9,0,11
931,Oxfordshire,9316076,123298,Our Lady's Abingdon,Abingdon,57,57,SUPP,SUPP,16
""")
df = pd.read_csv(f, names=['A','B','C','D','E','F','G', 'H','I','J', 'K'],
                 header=None)
df.replace('SUPP', 3.0, inplace=True)
df = df.convert_objects(convert_numeric=True)
df['KG'] = df['K']*1.0/df['G']
plt.plot(df['KG'], marker='o')

l, = plt.plot(df['KG'], marker='o', linestyle='', visible=False)
datacursor(l, hover=True, point_labels=df['E'], bbox=dict(fc='white'),
           formatter=lambda **kwargs: kwargs['point_label'][0], xytext=(0, 25))

plt.show()

相关问题 更多 >