gpt4 book ai didi

python - 我想在plotly(python)中将数据显示为x轴上的百分比

转载 作者:行者123 更新时间:2023-12-01 07:45:26 34 4
gpt4 key购买 nike

我想在 x 轴上以百分比形式显示数据。

我尝试了不同的方法,包括#tickformat方法,但无法成功获得所需的输出。

trace4 = go.Bar(
y=[''],
x=[5731],
name='Other(Used/Available)',
text = ("5731"),
textposition = 'inside',
orientation = 'h',
marker = dict(
color = 'rgba(210, 105, 30, 0.6)',
line = dict(
color = 'rgba(210, 105, 30, 1.0)',
width = 3)
)
)
data = [trace1, trace2, trace3, trace4]
layout = go.Layout(
autosize=False,
width=800,
height=275,
barmode='stack',
xaxis=dict(tickformat="%")
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='marker-h-bar')

最佳答案

好吧,这个是我自己想出来的。我计算了总数,然后这样计算了百分比。之后,只需使用 xaxis=dict(ticksuffix="%") 来分配“%”符号。

l = []

for i in rows2:
l.append(list(i))

schema = [i[0] for i in l]

nos = [i[1] for i in l]

nos = [int(x) for x in nos]

i = 0
t = 0
while i < len(nos):
t += nos[i]
i += 1

P = [float(x/t*100) for x in nos]

trace1 = go.Bar(
y=[''],
x=[P[0]],
name = (f"{schema[0]}"),
text = (f"{nos[0]}"),
textposition = 'auto',
orientation = 'h',
marker = dict(
color = 'rgba(103, 240, 160, 0.75)',
line = dict(
color = '#43BD78',
width = 3)
)
)
trace2 = go.Bar(
y=[''],
x=[P[1]],
name=(f"{schema[1]}"),
text = (f"{nos[1]}"),
textposition = 'auto',
orientation = 'h',
marker = dict(
color = 'rgba(255, 172, 188, 0.75)',
line = dict(
color = '#E67188',
width = 3)
)
)
trace3 = go.Bar(
y=[''],
x=[P[2]],
name=(f"{schema[2]}"),
text = (f"{nos[2]}"),
textposition = 'auto',
orientation = 'h',
marker = dict(
color = 'rgba(177, 193, 216, 1.0)',
line = dict(
color = '#82A1D0',
width = 3)
)
)
trace4 = go.Bar(
y=[''],
x=[P[3]],
name=(f"{schema[3]}"),
text = (f"{nos[3]}"),
textposition = 'auto',
orientation = 'h',
marker = dict(
color = 'rgba(250, 184, 126, 0.75)',
line = dict(
color = '#DF8E47',
width = 3)
)
)
trace5 = go.Bar(
y=[''],
x=[P[4]],
name=(f"{schema[4]}"),
text = (f"{nos[4]}"),
textposition = 'inside',
orientation = 'h',
marker = dict(
color = '#C8A8CD',
line = dict(
color = '#9C7DA1',
width = 3)
)
)
data = [trace1, trace2, trace3, trace4, trace5]
layout = go.Layout(
autosize=False,
width=700,
height=110,
barmode='stack',
xaxis=dict(ticksuffix="%"),
legend=dict(
orientation="h",
x=0.05,
y=-0.27,
font=dict(
size=10.5
)
),
margin=go.layout.Margin(
l=60,
r=60,
b=0,
t=0,
pad=0
)
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='marker-h-bar')

关于python - 我想在plotly(python)中将数据显示为x轴上的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56485994/

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