gpt4 book ai didi

python - 如何在条形图中同时设置宽度和间隙? ( python ,剧情)

转载 作者:行者123 更新时间:2023-12-05 04:53:50 28 4
gpt4 key购买 nike

我知道如何设置条形图中条形的宽度:

fig = go.Figure(data=[go.Bar(x = ['A', 'B', 'C'], y = [2, 4, 1], width=[0.5]*3)])
fig.update_layout(paper_bgcolor = 'rgba(0,0,0,0)',plot_bgcolor = 'rgba(0,0,0,0)')
fig.update_layout(bargap=0)
fig.show()

enter image description here

如您所见,bargap 被忽略了。因为条的宽度已经设置好了。现在,通过删除 width 属性,我们将得到以下内容:

enter image description here

我希望通过消除差距,同时拥有更细的条形图。现在,问题是:

"How to set width and gap simultaneously in a bar chart through plotly in python?"

最佳答案

有一个解决方法。在 https://plotly.com/python/bar-charts/ 查看 marimekko 示例

import plotly.graph_objects as go
import numpy as np

labels = ['A', 'B', 'C']
widths = np.array([0.5]*3)
widths[1] = 0.5*4

fig = go.Figure(data=[go.Bar(x=np.cumsum(widths)-widths, width=widths, offset=0, y = [2, 4, 1])])
fig.update_layout(paper_bgcolor = 'rgba(0,0,0,0)',plot_bgcolor = 'rgba(0,0,0,0)')
fig.update_xaxes(
showline = True, linecolor = '#000',
tickvals=np.cumsum(widths)-widths/2,
ticktext= ["%s" % (l) for l, w in zip(labels, widths)]
)
fig.update_yaxes(
showline = True, linecolor = '#000')
fig.update_layout()
fig.show()

Output

关于python - 如何在条形图中同时设置宽度和间隙? ( python ,剧情),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65938194/

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