Hover textbox dimensions

Hello,

I am generating a scatter plot with hover text on each point and the box always has only one line, meaning it is thin and long. I am trying to control the dimensions of the hover box so that it has a fixed horizontal width and its height changes to accommodate the text, but with no success. Based on the documentation, I passed a hoverlabel={‘width’:100}/{‘namelength’:100} but it did not have an effect on the result.

Below is the code block that generates the points. Any suggestions?

fig.add_trace( go.Scattergl( x=selection.x, y=selection.y, hovertext= selection.doc, hoverinfo="text", text=selection.text, mode='markers+text', name=name, textfont=dict( size=12, ), marker=dict(size=5, opacity=0.5) ) )

Hi @tryharder,

You can try to wrap the hovertext explicitly by creating custom function that return wrapped_hovertext series.

To do this you will need to import textwrap module.
The textwrap module in Python is an in-built module, so you don’t have to install any additional module.

Set how long the width of every line, by assigning integer value to width argument. I set at most 20 character long as default.

Then call this function and assign return value on hovertext keyword argument.

My suggestion based on your code will be

import textwrap

# wrap hovertext function, a line has at most width characters long.
def wrap_hovertext(hovertext, width=20):
    wrapped_hovertext = ["<br>".join(textwrap.wrap(text,width=width)) for text in hovertext]
    return wrapped_hovertext     

fig.add_trace( go.Scattergl( x=selection.x, y=selection.y, hovertext= wrap_hovertext(selection.doc), 
hoverinfo="text", text=selection.text, mode='markers+text', name=name, textfont=dict( size=12, ), 
marker=dict(size=5, opacity=0.5) ) )

Hope this help.