作为工具提示的容器不显示内容

2024-06-28 11:01:15 发布

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

我正在创建散点图,希望在工具提示中显示一些信息。在

以下方法非常有效:

import bqplot as bqp
import ipywidgets as ipw

xSc = bqp.LinearScale()
ySc = bqp.LinearScale()

tt = ipw.Label("A")
def hover_handler(self, content):
    tt.value = str(content)

s = bqp.Scatter(x=[0, 1, 2], y=[1, 2, 3], scales=dict(x=xSc, y=ySc),
                tooltip=tt)
s.on_hover(hover_handler)
bqp.Figure(marks=[s])

(没有轴之类的东西可以让代码简短)

将鼠标悬停在每个点上显示它的content很好。在

enter image description here

但是,我不想简单地显示原始内容。相反,我希望以表格形式显示它(但是默认的bqp.Tooltip不足以满足我的需要)。在

但是,如果我将标签包装在ipw.VBox中,工具提示就会变成一个很小的垂直条。添加min_widthmin_height会增加工具提示的大小,但是没有内容(即使使用默认值创建了tt)。如果我单独调用以单独显示VBox,则该版本将正常显示(即使没有定义布局),甚至在将鼠标移到这些点上时也会更新。在

^{pr2}$

enter image description here

如何使工具提示正确显示?在


Tags: 工具import内容ascontentminhandlertt
1条回答
网友
1楼 · 发布于 2024-06-28 11:01:15

我认为对于您正在尝试实现的目标,最好使用一个Output小部件作为工具提示,然后创建所需的小部件并使用output小部件作为上下文管理器显示它们。在

如果您想知道可以显示哪些附加信息,请尝试在hover_handler函数中打印content。在

    import bqplot as bqp
    import ipywidgets as ipw
    from IPython.display import display, clear_output

    xSc = bqp.LinearScale()
    ySc = bqp.LinearScale()


    out = ipw.Output()

    def hover_handler(self, content):
        out.clear_output()
        with out:
            label = ipw.Label(content['data']['name'])
            display(label)

    s = bqp.Scatter(x=[0, 1, 2], y=[1, 2, 3], scales=dict(x=xSc, y=ySc),
                    tooltip=out)
    s.on_hover(hover_handler)
    bqp.Figure(marks=[s])

要显示信息表,您可能需要一个ipy.HTML小部件,您可以使用它来传递HTML表。我有时用熊猫来做这个df.to_html格式()方法。在

相关问题 更多 >