Reputation:
In the code below, i have a dataframe with 4 columns ('data', 'valor', 'nome', and 'total'). The variable 'text' sets the column 'nome' as a label. Now i need to do the same with the column 'total', i need to set the column 'total' as a label too. But i can't figure out how.
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()
Something like ... :
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()
Upvotes: 4
Views: 2046
Reputation: 4032
The docs indicate you can't specify more than one column, but you can pass a list of strings.
Here's a self-contained little example that uses df.itertuples
in a list comprehension to create a string of text for each bar.
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)
Upvotes: 4