如何使用bqplot在散点图中插入工具提示

时间:2020-05-01 11:31:16

标签: python pandas tooltip scatter bqplot

我有一个熊猫数据框(df),其中有4列,分别名为“名称”,“ nb”,“ a”和“ b”,用其姓名,身份证号码('nb')和与之相关的一些值(“ a”和“ b”)。

template <typename... Args>
size_t x = sizeof...(Args);

我想用“ a”和“ b”值创建一个散点图,其中名称和ID会出现在工具提示中。我想我可以使用bqplot库来做到这一点。

我写了以下代码:

import pandas as pd
data = {
    "name": ["Thomas", "John", "Anna", "Ben", "Arlette"],
    'nb': [1,2,3,4,5],
    "a": [0, 2, 13, 43, 90],
    "b": [4, 24, 31, 2, 3],
}
df = pd.DataFrame(data)

尽管显示了标签,但工具提示为空。我想我在def_tt行中缺少某些内容,字段参数可能不正确。

2 个答案:

答案 0 :(得分:1)

使用bqplot工具提示,您只能引用实际标记中的字段。所以这样的事情应该起作用:

implementation 'androidx.appcompat:appcompat:1.2.0-beta01'

如果要从数据框中选取其他字段,则需要

1)创建一个输出小部件

2)定义当您将鼠标悬停在标记上时要运行的函数

3)将输出小部件设置为标记工具提示

4)设置该功能以将鼠标悬停在输出小部件中显示您的元数据 标记。

def_tt = Tooltip(fields=['index', 'x', 'y'], formats = ['','.2f'],)

enter image description here

答案 1 :(得分:1)

您还可以在数据框中创建一个新列,将其用作名称并在工具提示中显示该名称:

fig = plt.figure()

def_tt = Tooltip(fields=['name'], show_labels=False)

df['labels'] = df.apply(lambda row: f'X: {row.x:%Y-%m-%d}, Y: {row.y:.2f}, Other Info: {row.custom_field}', axis=1)

scatt = plt.scatter(df.x, df.y, tooltip=def_tt,
                    # specify labels, but hide by default
                    names=df.labels, display_names=False)
fig

enter image description here

但它不支持富格式。甚至没有换行:(