@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:
- 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_
- 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