I am trying to generate a random 3D scatter plot where each point has a label. How can I add these labels? I would like to add them inside the yellow box.
Since you're specifically asking how to
add them inside the yellow box
you're not really asking how to annotate a 3D chart, which you could otherwise do with 3D annotations, but really how to customize the hover info. If you're willing to use plotly.express
, you can use custom_data
in px.scatter_3D()
to include information about a fourth variable not displayed in the scatterplot:
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='petal_length', size='petal_length', size_max=18,
symbol='species', opacity=0.7,
custom_data = ['category']
)
temp1 = fig.data[0].hovertemplate
fig.update_traces(hovertemplate = temp1 + '<br>' + "Category: %{customdata[0]}")
import plotly.express as px
df = px.data.iris()
category = {'setosa':'flower', 'versicolor': 'vegetable', 'virginica': 'not a flower'}
df['category'] = df['species'].map(category)
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='petal_length', size='petal_length', size_max=18,
symbol='species', opacity=0.7,
custom_data = ['category']
)
temp1 = fig.data[0].hovertemplate
fig.update_traces(hovertemplate = temp1 + '<br>' + "Category: %{customdata[0]}")
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
fig.show()