问题
I'm creating a scatterplot and want to show some information in a tooltip.
The following works perfectly:
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])
(there's no axes and whatnot to keep the code short)
Hovering over each point shows its content
just fine.
However, I don't want to simply show the raw content. Instead, I want to show it in tabular form (but the default bqp.Tooltip
isn't sufficient for my needs).
However, if I wrap the label in a ipw.VBox
, the tooltip becomes a tiny vertical sliver. Adding a min_width
and min_height
increases the size of the tooltip, but there's no content (even though tt
was created with a default value). If I make a separate call to display the VBox alone, that version appears normally (even without defining the layout) and even updates when moving the mouse over the points.
import bqplot as bqp
import ipywidgets as ipw
from IPython.display import display
xSc = bqp.LinearScale()
ySc = bqp.LinearScale()
tt = ipw.Label("A")
vb = ipw.VBox(children=[tt], layout=ipw.Layout(min_width='100px', min_height='100px'))
display(vb)
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=vb)
s.on_hover(hover_handler)
bqp.Figure(marks=[s])
What do I need to do for the tooltip to appear correctly?
回答1:
I think for what you are trying you achieve you would be better served with an Output
widget as a tooltip, and then create the widgets you need and display them using the output widget as a context manager.
If you want to see what additional information could be presented, try printing content
within your hover_handler
function.
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])
To display a table of information, you probably need an ipy.HTML
widget which you can use to pass a HTML table. I sometimes use pandas for this, with the df.to_html() method.
来源:https://stackoverflow.com/questions/56655741/container-as-tooltip-doesnt-show-contents