Reputation: 728
So I'm creating a plotly bar chart that is stacked. Each of my stacks are returning the same color. I'd like them to have different colors. I'd prefer not to use plotly express in this situation. I also don't want to specify the color. I just want a vareity of colors. Here's my code:
df2 = pd.DataFrame.from_dict({'Country': {0: 'Europe',
1: 'America',
2: 'Asia',
3: 'Europe',
4: 'America',
5: 'Asia',
6: 'Europe',
7: 'America',
8: 'Asia',
9: 'Europe',
10: 'America',
11: 'Asia'},
'Year': {0: 2014,
1: 2014,
2: 2014,
3: 2015,
4: 2015,
5: 2015,
6: 2016,
7: 2016,
8: 2016,
9: 2017,
10: 2017,
11: 2017},
'Amount': {0: 1600,
1: 410,
2: 150,
3: 1300,
4: 300,
5: 170,
6: 1000,
7: 500,
8: 200,
9: 900,
10: 500,
11: 210}})
fig = go.Figure()
fig.add_trace(go.Bar(x=df2['Year'], y=df['Amount'],
))
fig.update_layout(title="Personnel at Work",
barmode='stack')
fig.show()
Upvotes: 0
Views: 848
Reputation: 35135
To create a stacked graph with a graph object, you can specify the graph as the stacking unit.
import pandas as pd
import plotly.graph_objects as go
fig = go.Figure()
x = [str(x) for x in df2['Year'].unique().tolist()]
for c in df2['Country'].unique():
df3 = df2.query('Country == @c')
fig.add_trace(go.Bar(x=x, y=df3['Amount'], name=c))
fig.update_layout(title="Personnel at Work", barmode='stack')
fig.show()
Upvotes: 1