python - Plotly 3D 绘图注释

标签 python annotations plotly label

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

enter image description here

最佳答案

既然您专门询问如何

add them inside the yellow box

您并不是真正询问如何注释 3D 图表,否则您可以使用 3D annotations 来完成此操作,但实际上如何自定义悬停信息。如果您愿意使用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]}")

enter image description here

完整代码:

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

关于python - Plotly 3D 绘图注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68262505/

相关文章:

java - 如何从注释处理器创建文件名中带有空格的资源

python - 在 Plotly Python 中创建一个文本框

python - 调整打印函数格式以跳过缺失值

python - "maximum recursion depth exceeded"当 "property"应用于实例变量 "self.x"

Python Pandas : How to select the index of the min from one column and use that to select index for other columns after a grouby

python - Pandas:过滤后的数据帧不返回任何行,但未过滤的数据框会返回任何行

spring - Spring 有没有像温莎城堡那样的@predestroy

IOS uimap 单击 subview 中的标注按钮

r - 在 R plotly d3js 如何用空格而不是逗号格式化数字?

javascript - plotly :如何丢弃旧点?