Plotly 3D 绘图注释

Plotly 3D plot annotations

我正在尝试生成一个随机 3D 散点图,其中每个点都有一个标签。如何添加这些标签?我想将它们添加到黄色框内。

既然你是专门问如何

add them inside the yellow box

你并不是真的在问如何注释 3D 图表,否则你可以用 3D annotations, but really how to customize the hover info. If you're willing to use plotly.express 来做,你可以在 px.scatter_3D() 中使用 custom_data 来包含关于第四个的信息变量未显示在散点图中:

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()