【问题标题】:Change hover text of a plotly express treemap更改绘图表达树状图的悬停文本
【发布时间】:2020-11-05 09:32:35
【问题描述】:

我想在树形图中只显示每个项目的标签和值,而不是父项或 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()

我也尝试使用非表达树图创建它。 Hovertext 是我想要的,但是具有两个级别的树形图呈现为不对称的。

我想要的是类似于非表达树图的悬停文本,但与表达树图一样平衡和对称

我能做什么?

提前致谢!

【问题讨论】:

标签: python plotly plotly-python plotly-express


【解决方案1】:

在我看来你应该覆盖你的悬停模板

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

【讨论】:

    猜你喜欢
    • 2019-05-30
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    相关资源
    最近更新 更多