gpt4 book ai didi

python-3.x - 剧情( python ): How to add more then one text label in a bar chart

转载 作者:行者123 更新时间:2023-12-05 03:45:23 27 4
gpt4 key购买 nike

在下面的代码中,我有一个包含 4 列(“data”、“valor”、“nome”和“total”)的数据框。变量 'text' 将列 'nome' 设置为标签。现在我需要对“总计”列做同样的事情,我也需要将“总计”列设置为标签。但我不知道该怎么做。

  def bar_detalhado(dframe):
import plotly.express as px
import plotly

df = dframe
fig = px.bar(df, x="data", y="valor", text="nome",
color='tipo', barmode='group',
height=400)
fig.show()

类似...:

def bar_detalhado(dframe):
import plotly.express as px
import plotly

df = dframe
fig = px.bar(df, x="data", y="valor", text="nome", text2="total",
color='tipo', barmode='group',
height=400)
fig.show()

最佳答案

文档指出您不能指定多于一列,但您可以传递一个字符串列表。

这是一个独立的小示例,它在列表理解中使用 df.itertuples 为每个栏创建一个文本字符串。

import plotly.express as px
import pandas as pd

df = pd.DataFrame(dict(data=[1, 2, 3], valor=[5, 6, 7], nome=[8, 9, 10]))
df['total'] = df.sum(axis=1)
df


def bar_detalhado(dframe):
fig = px.bar(
dframe,
x="data",
y="valor",
text=[f"n={row[2]} v={row[1]} t={row[3]}" for row in df.itertuples()],
)
fig.show()

bar_detalhado(df)

关于python-3.x - 剧情( python ): How to add more then one text label in a bar chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65861823/

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