1

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.

enter image description here

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

enter image description here

What do I need to do for the tooltip to appear correctly?

Wasabi
  • 2,879
  • 3
  • 26
  • 48

1 Answers1

0

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.

ac24
  • 5,325
  • 1
  • 16
  • 31
  • Yeah, my current workaround is precisely an `ipy.HTML` widget which I hardcoded with a table. I was unaware of `df.to_html`, will check it out, and will see how the tooltip looks when wrapped in an `ipw.Output` (don't have access to the code today). – Wasabi Jun 20 '19 at 12:50
  • This isn't working, unfortunately. I just get a blank (but non-zero sized) tooltip, even if I change the label to simply `ipw.Label('A')`. [See this screenshot](https://i.stack.imgur.com/JVYvT.png). – Wasabi Jun 21 '19 at 14:45
  • Which version of ipywidgets and bqplot are you working with? – ac24 Jun 21 '19 at 15:25
  • ipywidgets 7.3.2 and bqplot 0.11.4 – Wasabi Jun 21 '19 at 16:10
  • I'm using ipywidgets 7.4.2 and bqplot 0.11.4. I'm using Chrome browser, not sure if that makes a difference. – ac24 Jun 24 '19 at 07:34