简体   繁体   English

制作不同颜色的堆叠条形图

[英]Make Stack Bar Chart Plotly Different Colors

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.在这种情况下,我不想使用 plotly express 。 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()

在此处输入图片说明

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM