gpt4 book ai didi

python - 如何在 Plotly 中的特定条形之间添加间隙

转载 作者:行者123 更新时间:2023-12-05 03:38:50 24 4
gpt4 key购买 nike

我使用 Plotly Express 从 px.bar(...) 生成了以下条形图: Bar chart

我想在 10 条和“全部”条之间添加一个垂直间隙以区分“全部”条。有没有办法在 Plotly 中做到这一点?

最佳答案

据我所知,没有直接的方法可以做到这一点。作为替代解决方案,我会考虑拆分数据并将您的图形设置为两个 subplots .您需要添加一些自定义项,例如 shared_xaxes=True,并通过 row_heights=[0.2, 0.8] 调整行高以适合您的用例。使用内置数据集 px.data.tips 下面的代码片段使用类似这样的方法生成以下图:

enter image description here

完整代码:

import plotly.express as px
from plotly.subplots import make_subplots
from itertools import cycle

df = px.data.tips()

fig1 = px.bar(df[df.day != 'Sun'], x="total_bill", y="day", orientation='h')
fig2 = px.bar(df[df.day == 'Sun'], x="total_bill", y="day", orientation='h')

f = fig2.full_figure_for_development(warn=False)

fig = make_subplots(rows=2, cols=1, shared_xaxes=True, row_heights=[0.2, 0.8])
fig.update_layout(template = 'plotly_white')
fig.update_xaxes(showgrid = False)

fig.add_traces(fig2.data, 1, 1)
fig.add_traces(fig1.data, 2,1)
fig.show()

关于python - 如何在 Plotly 中的特定条形之间添加间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68838638/

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