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

标签 python plotly

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

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/

相关文章:

python - Matplotlib:如何在不循环的情况下绘制彩色点?

python - 从使用嵌套字典创建的数据帧设置 Pandas 分层多索引

r - 在 R 中自定义 infoWindow/tooltip --> plotly

python - 属性错误: module 'plotly.tools' has no attribute 'set_credentials_file'

python - Bokeh - 子类图

python - 用 hexbin 覆盖多组数据

R图表转换为html格式,无需其他文件

python - 绘制 x 轴值与输入值不同

R plotly子图在图之间添加空间

python - 如何从图像创建视频?