Plotly漏斗 - 不想显示“总百分比”,但想要“初始百分比”和“前一百分比”
在下面的图片中,你可以看到鼠标悬停时显示了“总数的百分比”。这个数字是所有层级加起来的百分比,但在这个漏斗图中其实并不重要。
下面是生成悬停信息的代码:
def create_engagement_figure(funnel_data=[]):
fig = go.Figure(
go.Funnel(
y=funnel_data["Title"],
x=funnel_data["Count"],
textposition="auto",
textinfo="value+percent initial+percent previous",
# opacity=0.65,
marker={
"color": [
"#4F420A",
"#73600F",
"#947C13",
"#E0BD1D",
"#B59818",
"#D9B61C",
],
"line": {
"width": [4, 3, 2, 2, 2, 1],
"color": ["wheat", "wheat", "wheat", "wheat"],
},
},
connector={"line": {"color": "#4F3809", "dash": "dot", "width": 3}},
)
)
fig.update_traces(texttemplate="%{value:,d}")
fig.update_layout(
margin=dict(l=20, r=20, t=20, b=20),
)
return fig
特别是这一行:
textinfo="value+percent initial+percent previous",
我试过:
textinfo="value+percent",
还有:
textinfo="value",
但无论怎么改,还是会显示“总数的百分比”。请问有没有办法通过plotly库去掉这个显示,这样我就不需要自己去做一个自定义的悬停信息?因为自定义悬停信息会有问题,因为这段代码会根据传入的数据框生成多个层级的漏斗图。
不太相关的,这里是其余的代码:
def engagement_funnel_chart(daterange, countries_list):
LR = metrics.get_totals_by_metric(daterange, countries_list, stat="LR")
PC = metrics.get_totals_by_metric(daterange, countries_list, "PC")
LA = metrics.get_totals_by_metric(daterange, countries_list, stat="LA")
GC = metrics.get_totals_by_metric(daterange, countries_list, "GC")
funnel_data = {
"Title": [
"Learners Reached",
"Puzzle Completed",
"Learners Acquired",
"Game Completed",
],
"Count": [LR, PC, LA, GC],
}
fig = create_engagement_figure(daterange, countries_list, funnel_data)
st.plotly_chart(fig, use_container_width=True)
1 个回答
1
你可以自定义 hoverinfo
属性(就像 textinfo
一样,不需要完全重写 hovertemplate
)。默认值是 'all'
,这对应于标志列表:
'x+y+text+percent initial+percent previous+percent total'
如果你想去掉 percent total
这个标志,比如说:
fig = go.Figure(
go.Funnel(
y = ["Website visit", "Downloads", "Potential customers", "Requested price", "Finalized"],
x = [39, 27.4, 20.6, 11, 2],
textposition="auto",
textinfo="value+percent initial+percent previous",
hoverinfo='x+y+text+percent initial+percent previous',
# opacity=0.65,
marker={
"color": [
"#4F420A",
"#73600F",
"#947C13",
"#E0BD1D",
"#B59818",
"#D9B61C",
],
"line": {
"width": [4, 3, 2, 2, 2, 1],
"color": ["wheat", "wheat", "wheat", "wheat"],
},
},
connector={"line": {"color": "#4F3809", "dash": "dot", "width": 3}},
)
)