Black Lives Matter. Please consider donating to Black Girls Code today.

Scatter3d "hovertemplate"

Hi,

I am using plotly express to create a 3D scatter plot of document embeddings.

I would like the hover to just show me the document title, not the coordinates.

I have tried a lot of methods from the docs unsuccessfully. I thought “hovertemplate” from the documentation was the way to go, but I am getting:
TypeError: scatter_3d() got an unexpected keyword argument 'hovertemplate'

Any ideas?
Thanks!

@PabloRR10 In think that you have an old Plotly version because Plotly 4.5.2 provides this property for go.Scatter3d

help(go.Scatter3d.hovertemplate)
displays:


    Template string used for rendering the information that appear
    on hover box. Note that this will override `hoverinfo`.
    Variables are inserted using %{variable}, for example "y:
    %{y}". Numbers are formatted using d3-format's syntax
    %{variable:d3-format}, for example "Price: %{y:$.2f}".
    https://github.com/d3/d3-3.x-api-
    reference/blob/master/Formatting.md#d3_format for details on
    the formatting syntax. Dates are formatted using d3-time-
    format's syntax %{variable|d3-time-format}, for example "Day:
    %{2019-01-01|%A}". https://github.com/d3/d3-3.x-api-
    reference/blob/master/Time-Formatting.md#format for details on
    the date formatting syntax. The variables available in
    `hovertemplate` are the ones emitted as event data described at
    this link https://plot.ly/javascript/plotlyjs-events/#event-
    data. Additionally, every attributes that can be specified per-
    point (the ones that are `arrayOk: true`) are available.
    Anything contained in tag `<extra>` is displayed in the
    secondary box, for example "<extra>{fullData.name}</extra>". To
    hide the secondary box completely, use an empty tag
    `<extra></extra>`.
    
    The 'hovertemplate' property is a string and must be specified as:
      - A string
      - A number that will be converted to a string
      - A tuple, list, or one-dimensional numpy array of the above
    
    Returns
    -------
    str|numpy.ndarray

Hi @empet, thanks for the reply!

I am using plotly version 4.5.2. and I can also see that information calling the help function.

My implementation right now is like this:

fig = px.scatter_3d(
    d, x='d1', y='d2', z='d3', 
    color='cluster',     # hover_name='Title', hover_data=["ID"]) 
    hovertemplate="T: %{Title} | ID: %{ID}")

And the error is the same:

* #### File "/app/clustering__viz/clustering_viz.py", line  *275* , in  `update_umap`

hovertemplate="T: %{Title} | ID: %{ID}")

> TypeError: scatter_3d() got an unexpected keyword argument 'hovertemplate'

Does it have a different use with Plotly Express?

@PabloRR10,

Before working with plotly express you should learn how plotly.py works, what are the properties of a go.Figure i.e. what contains a Plotly figure:

  1. it contains data, a list of traces of different types. To get the number of traces,
    type:

print(len(fig.data)),

then inspect each trace:

fig.data[0], fig.data[1], etc to see its type (i.e. scatter, scatter3d, heatmap, etc_

  1. The second component of a plotly figure is its layout:

fig.layout

plotly.express creates a figure by calling a function. In your case this function is px.scatter_3d(but it isnt an instance of the class go.Scatter3d)
To find out what traces contains the fig returned by px.scatter_3d, type
fig.data[0], as I mentioned above.

Let us consider this example:

import plotly.express as px
df = px.data.election()
fig = px.scatter_3d(df, x="Joly", y="Coderre", z="Bergeron", color="winner", size="total", hover_name="district",
                  symbol="result", color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"})
fig.show()

len(fig.data) is 6

fig.data[0] is:

Scatter3d({
    'hoverlabel': {'namelength': 0},
    'hovertemplate': ('<b>%{hovertext}</b><br><br>win' ... 'n=%{z}<br>total=%{marker.size}'),
    'hovertext': array(['101-Bois-de-Liesse', '102-Cap-Saint-Jacques',
                        '161-Saint-HenriPetite-BourgognePointe-Saint-Charles',
                        '172-Desmarchais-Crawford', '181-Peter-McGill', '182-Saint-Jacques',
                        '33-Snowdon', '34-Notre-Dame-de-Grâce', '35-Loyola', '41-du Canal',
                        '43-Fort-Rolland', '63-Jacques-Bizard'], dtype=object),
    'legendgroup': 'Joly, plurality',
    'marker': {'color': 'blue',
               'size': array([7334, 6363, 9378, 7956, 4099, 6357, 4687, 7688, 6125, 3263, 4438, 1432],
                             dtype=int64),
               'sizemode': 'area',
               'sizeref': 27.625,
               'symbol': 'circle'},
    'mode': 'markers',
    'name': 'Joly, plurality',
    'scene': 'scene',
    'showlegend': True,
    'x': array([3024, 2675, 3578, 2849, 1894, 2282, 1636, 3262, 2648, 1266, 1908,  690],
               dtype=int64),
    'y': array([2481, 2525, 2432, 2476, 1451, 1906, 1548, 1773, 2040, 1165, 1325,  518],
               dtype=int64),
    'z': array([1829, 1163, 3368, 2631,  754, 2169, 1503, 2653, 1437,  832, 1205,  224],
               dtype=int64)
})

If you want to change the hovertemplate for this Scatter3d trace perform
an update:

fig.data[0].update(hovertemplate='your new template')

and similarly for other trace types.

Hence, don’t pass hovertemplate as an argument for the function px.scatter_3d!!! hovertemplate is an attribute of `go.Scatter go.Scatter3d, go.Heatmap, go.Bar, etc