更改Plotly Express TreeMap的悬停文本

6

我想在矩形树图中仅显示每个项目的标签和值,而不是父级或ID。 我已经使用plotly express定义了它。无论我如何调整,都无法将悬停文本限制为我需要的字段。请检查代码和截图。

import plotly.express as px

fig = px.treemap(dfconcepto, path=['type','name'], 
                 values = 'count',
                 width=900, height=900,
                 hover_data = ['count'],
)

fig.show()

image
image
1117×957 44.9 KB 我也尝试使用非express treemap创建它。悬停文本是我想要的,但是一个有两个级别的treemap呈不对称状态。

enter image description here

我想要的是类似于非express treemap的悬停文本,但像express treemap一样平衡和对称。

我该怎么办呢?

提前感谢!


你介意分享 dfconcepto 以便我们可以复现它吗? - rpanai
1
https://gist.github.com/jlchulilla/3b4e40f68ba73b5dbcb661a1d861f308 - Juan Luis Chulilla
1个回答

10

我认为你应该覆盖你的悬停模板

import pandas as pd
import plotly.express as px
url = "https://gist.githubusercontent.com/jlchulilla/3b4e40f68ba73b5dbcb661a1d861f308/raw/e564973db30a4612aba60c5b26dd108edc98f048/test2sof.csv"

df = pd.read_csv(url).drop("Unnamed: 0", axis=1)

fig = px.treemap(df, path=['type','name'], 
                 values = 'coincidencia',
                 width=900, height=900,
)

# Now your hovertemplate looks like
# fig.data[0].hovertemplate 
# 'labels=%{label}<br>coincidencia=%{value}<br>parent=%{parent}<br>id=%{id}<extra></extra>'

# But it seems to me you want something like
fig.data[0].hovertemplate = '%{label}<br>%{value}'
fig.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接