如何在具有multiIndex的数据框中按特定级别在堆叠区域图中设置颜色?

时间:2018-11-07 17:42:35

标签: plot colors multi-index stacked-area-chart

我有一个要在堆积面积图中绘制的具有MultiIndex的数据框。我想为每个区域(MultiIndex)设置颜色,例如:1)A,C1; 2)A,C2; 3)A,C3,4)B,C1; 5)B,C2; 6)B,C3。

                         MultiIndex((levels=[['A', 'B'], ['C1, C2', 'C3']]
                         labels=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]],
                         names=['plant', 'product'])
df

plant   A                                   B
product C1          C2          C3          C1          C2          C3
2013    91.819787   169.067457  96.622599   101.945246  156.474884  4.034889
2014    90.717770   169.067457  88.176969   101.945246  156.474884  4.034889
2015    90.717770   168.782780  87.464522   101.945246  156.474884  4.034889
2016    90.717770   166.585114  87.205738   101.945246  156.474884  4.034889
2017    88.869848   165.472301  86.936643   101.945246  156.474884  4.034889
2018    88.628546   165.336691  82.474329   101.945246  156.474884  4.034889
2019    81.971873   164.165038  82.258477   101.945246  156.474884  4.034889
2020    81.432714   162.372458  82.054170   101.945246  156.474884  4.034889
2021    80.575571   162.283999  82.050511   101.945246  156.474884  4.034889

我尝试过

ax = df.plot(kind='area', rot= 0, stacked=True, figsize=(12, 5), color= 
{"b","darkblue", "olive", "green", "pink", "cornflowerblue"}),

但是在颜色功能中,我想显式设置MultiIndex。假设对于[A,C1]我想要深蓝色,依此类推。我该怎么办?

0 个答案:

没有答案