How can I edit what is displayed upon hovering over data points in plotly graphs?

I have made a plotly chart which looks like so:

image

As can be seen, when hovering over a data point, it displays the feature name from the dataset (which I do using the name and text parameters in the data field of the graph). However, I want to remove those numbers that appear alongwith the feature names upon hovering over the data points, and if possible replace them with some other info from the dataset about that data point. How can I do that? Even if I make the name and text parameters blank, those numbers do not go away; so far I have been able to make them disappear only with hovermode=False, which is not what I seek.

Also, how do I remove the y-axis numbers (the x-axis should be retained as is) and the grid lines from the plot?

@kristada619,

  1. To remove the default displayed data on hover, define a list of strings and values for each point
    and set in trace definition, hoverinfo=β€˜text’:
y=2+0.5*np.random.random(20)


data = [go.Scatter(x=np.linspace(0,1, 20),
                   y=y,
                   mode='markers',
                   marker=dict(size=8, color='red'),
                   text=[f'Hello!<br>my value: {round(yval,2)}' for yval in y],
                   hoverinfo='text')]

2-3) Define the xaxis, yaxis dicts as follows:

xaxis=dict(zeroline=False, showline=True, ticklen=4, showgrid=False),
yaxis=dict(visible=False)

2 Likes

@empet Thanks for your reply. I figured out point 1. About point 2, I get an error when trying to modify the xaxis and yaxis. One of the data points (there are 42 of them in my figure, data[0] through data[41]) looks like this:

   >>> plotly_fig['data'][1]

    Scatter({
        'hoverinfo': 'text',
        'marker': {'color': [rgba(173,71,134,1.0), rgba(173,71,134,1.0),
                             rgba(101,103,181,1.0), ..., rgba(245,39,87,1.0),
                             rgba(173,71,134,1.0), rgba(173,71,134,1.0)],
                   'line': {'color': [rgba(173,71,134,1.0), rgba(173,71,134,1.0),
                                      rgba(101,103,181,1.0), ..., rgba(245,39,87,1.0),
                                      rgba(173,71,134,1.0), rgba(173,71,134,1.0)],
                            'width': 0},
                   'size': 4.0,
                   'symbol': 'circle'},
        'mode': 'markers',
        'name': '',
        'text': 'Some text',
        'uid': '3c00562c-5200-11e9-83b6-a0d37adb4cf4',
        'x': [0.006995890289545059, 0.00341446022503078, -0.003829206805676222, ...,
              -0.008903387933969498, 0.004270920529961586, 0.00910072959959507],
        'xaxis': 'x',
        'y': [0.16, -0.13, 0.18, ..., 0.02, 0.0, 0.09],
        'yaxis': 'y'
    })

Then when I try do the following:

plotly_fig['data'][1]['xaxis'] = dict(zeroline=False, showline=True, ticklen=4, showgrid=False)

I get the following error:

ValueError: 
    Invalid value of type 'builtins.dict' received for the 'xaxis' property of scatter
        Received value: {'ticklen': 4, 'showgrid': False, 'zeroline': False, 'showline': True}

    The 'xaxis' property is an identifier of a particular
    subplot, of type 'x', that may be specified as the string 'x'
    optionally followed by an integer >= 1
    (e.g. 'x', 'x1', 'x2', 'x3', etc.)

The complete stacktrace is as follows:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-b62b69100e98> in <module>()
----> 1 plotly_fig['data'][1]['xaxis'] = dict(zeroline=False, showline=True, ticklen=4, showgrid=False)

c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value)
   2773             # ### Handle simple property ###
   2774             else:
-> 2775                 self._set_prop(prop, value)
   2776 
   2777         # Handle non-scalar case

c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\plotly\basedatatypes.py in _set_prop(self, prop, val)
   3009                 return
   3010             else:
-> 3011                 raise err
   3012 
   3013         # val is None

c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\plotly\basedatatypes.py in _set_prop(self, prop, val)
   3004         validator = self._validators.get(prop)
   3005         try:
-> 3006             val = validator.validate_coerce(val)
   3007         except ValueError as err:
   3008             if self._skip_invalid:

c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v)
   1423             pass
   1424         elif not isinstance(v, string_types):
-> 1425             self.raise_invalid_val(v)
   1426         else:
   1427             # match = re.fullmatch(self.regex, v)

c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\_plotly_utils\basevalidators.py in raise_invalid_val(self, v)
    242             typ=type_str(v),
    243             v=repr(v),
--> 244             valid_clr_desc=self.description()))
    245 
    246     def raise_invalid_elements(self, invalid_els):

ValueError: 
    Invalid value of type 'builtins.dict' received for the 'xaxis' property of scatter
        Received value: {'ticklen': 4, 'showgrid': False, 'zeroline': False, 'showline': True}

    The 'xaxis' property is an identifier of a particular
    subplot, of type 'x', that may be specified as the string 'x'
    optionally followed by an integer >= 1
    (e.g. 'x', 'x1', 'x2', 'x3', etc.)

@kristada619

xaxis, yaxis are dicts in layout.
Hence you should make updates as follows:

plotly_fig['layout']['xaxis'].update(zeroline=False, showline=True, ticklen=4, showgrid=False)
and analogously for yaxis.

1 Like

@empet Oh, I see, my bad. Thanks a ton!