gpt4 book ai didi

python - 绘制两个重叠的漏斗 : the code isn't working

转载 作者:行者123 更新时间:2023-12-05 05:41:23 25 4
gpt4 key购买 nike

我想可视化两个重叠的漏斗:蓝色(较宽的漏斗)和红色。我想要蓝色一个有图例,数字,轴不可见。所以我尝试了这段代码(不起作用):

from plotly import graph_objects as go
from plotly.subplots import make_subplots


trace=go.Funnel(
name = 'all',
y = ["all leads", "not junks", "warms", "hots","deals"],
x = [317379,304725,11476,3194,2163],
textposition=None,
marker={'xaxis': {'range': [0.2, 1],
'showgrid': False,
'zeroline': False,
'visible': False}})


trace1=go.Funnel(
name = 'web',
y = ["all leads", "not junks", "warms", "hots","deals"],
x = [281316,269490,10252,2508,1602],
textposition='outside',
marker={"color": '#dc143c',"colorscale": 'Hot',"colorbar": {"bgcolor": None}})

fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(trace)
fig.add_trace(trace1,secondary_y=True)

fig.show()

出现错误:错误的属性路径

问题在于:

marker={'xaxis': {'range': [0.2, 1],
'showgrid': False,
'zeroline': False,
'visible': False}})

我不知道如何修复,但我需要它来使蓝色的图例、数字和轴不可见。没有这个代码就可以工作,但返回这个结果: enter image description here这不是我所期望的,因为它不清楚

最佳答案

如果我正确理解了您要实现的目标,那么只需对您的代码进行一些修改:

from plotly import graph_objects as go
from plotly.subplots import make_subplots

trace=go.Funnel(
name = 'all',
y = ["all leads", "not junks", "warms", "hots","deals"],
x = [317379,304725,11476,3194,2163],
textposition=None,
showlegend=False,
marker={"color": 'blue'}
)

trace1=go.Funnel(
name = 'web',
y = ["all leads", "not junks", "warms", "hots","deals"],
x = [281316,269490,10252,2508,1602],
textposition='outside',
marker={"color": '#dc143c',"colorscale": 'Hot',"colorbar": {"bgcolor": None}})

fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(trace)
fig.add_trace(trace1, secondary_y=True)

fig.layout.yaxis.update(showticklabels=False)
fig.layout.xaxis.update({'visible': True})

fig.for_each_trace(
lambda trace: trace.update(ids=[]) if trace.name == "all" else (),
)

fig.show()

使用 showlegend=False 删除蓝色/更宽漏斗图的图例。使用 marker= 参数为该图表设置蓝色。使用 fig.layout.yaxis.update(showticklabels=False) 删除该图的 y 轴。 (您可以使用 fig.layout.yaxis2.update(...) 删除另一个 y 轴的标签。)并使用 fig.for_each_trace( ...)

X 轴也通过 fig.layout.xaxis.update({'visible': True}) 变为可见。

enter image description here

关于python - 绘制两个重叠的漏斗 : the code isn't working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72283281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com