简体   繁体   English

Plotly 3D 绘图注释

[英]Plotly 3D plot annotations

I am trying to generate a random 3D scatter plot where each point has a label.我正在尝试生成一个随机的 3D 散点图,其中每个点都有一个标签。 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.你并不是真的在问如何注释 3D 图表,否则你可以用3D 注释来做,而是真正问如何自定义悬停信息。 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:如果您愿意使用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]}")

在此处输入图片说明

Complete code:完整代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM