如何减少绘图中堆叠条形之间的条形组间隙

问题描述 投票:0回答:1

我正在使用以下代码使用plotly 生成分组堆叠条形图。我想减少分组条之间的间隙。我怎样才能实现这个目标?特别是,我想让条形彼此相邻,之间几乎没有间隙。

rand_list = np.random.rand(30) # update this with original 

df = pd.DataFrame(
    dict(
        week=[1.2,1.2,1.3,1.3,1.6,1.6,1.7,1.7,4.4,4.4] * 3,
        layout=["abc","abc","def","def", "ghi", "ghi"] * 5,
        response=["0", "1"] * 15,
        cnt= list(rand_list),
    )
)
print(df)
fig = go.Figure()
fig.update_layout(
    template="simple_white",
    xaxis=dict(title_text="x-label"),
    yaxis=dict(title_text="y-label", showticklabels=False),
    barmode="stack",
)
colors = ["#2A66DE", "#FFC32B"]

for r, c in zip(df.response.unique(), colors):
    plot_df = df[df.response == r]
    fig.add_trace(
        go.Bar(x=[plot_df.week, plot_df.layout], y=plot_df.cnt, name=r, marker_color=c, width=0.2),
    )

fig
python plotly
1个回答
0
投票

使用

fig.update_traces
怎么样

[...]

fig.update_traces(width=None)
fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.