Plotly 3D 绘图注释 [英] Plotly 3D plot annotations

查看:33
本文介绍了Plotly 3D 绘图注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

既然你特别想问如何

<块引用>

将它们添加到黄色框内

您并不是真的在问如何注释 3D 图表,否则您可以使用

完整代码:

import plotly.express as pxdf = px.data.iris()category = {'setosa':'flower','versicolor':'vegetable','virginica':'不是花'}df['category'] = df['species'].map(category)fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',颜色=花瓣长度",尺寸=花瓣长度",尺寸最大=18,符号='物种',不透明度=0.7,custom_data = ['类别'])temp1 = fig.data[0].hovertemplatefig.update_traces(hovertemplate = temp1 + '
' + "Category: %{customdata[0]}")fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))图.show()

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]}")

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

这篇关于Plotly 3D 绘图注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆