本文介绍了Seaborn 中的堆积条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

  countries2012 = ['玻利维亚','巴西','斯里兰卡','多明尼加共和国','印度尼西亚','肯尼亚','洪都拉斯',莫桑比克'秘鲁','菲律宾','印度','越南','泰国','美国','世界']百分比2012 = [0.042780099,0.16599952,0.012373058,0.019171717,0.011868674,0.019239173,0.00000332,0.014455196,0.016006654,0.132970981,0.077940824,0.411752517,0.017986798,0.017361808,0.058076027]国家 2013 = ['玻利维亚','巴西','斯里兰卡','多明尼加共和国','印度尼西亚','洪都拉斯','莫桑比克','秘鲁','菲律宾','印度','越南','泰国','美国','世界']2013 年百分比 = [0.02736294,0.117160272,0.015815952,0.018831589,0.020409103 ,0.00000000285,0.018876854,0.018998639,0.117221146,0.067991687,0.496110972,0.019309486,0.026880553,0.03503080414999993]

我想制作一个堆积条形图,以便有一个 2012 年的堆积条形图和另一个 2013 年的堆积条形图.

由于 2012 年和 2013 年的国家/地区不同,我该怎么做?

解决方案

IIUC,您可以创建

I have the following data:

countries2012 = [
    'Bolivia',
    'Brazil',
    'Sri Lanka',
    'Dominican Republic',
    'Indonesia',
    'Kenya',
    'Honduras',
    'Mozambique',
    'Peru',
    'Philipines',
    'India',
    'Vietnam',
    'Thailand',
    'USA',
    'World'
]

percentage2012 = [
    0.042780099,
    0.16599952,
    0.012373058,
    0.019171717,
    0.011868674,
    0.019239173,
    0.00000332,
    0.014455196,
    0.016006654,
    0.132970981,
    0.077940824,
    0.411752517,
    0.017986798,
    0.017361808,
    0.058076027
]

countries2013 = [
    'Bolivia',
    'Brazil',
    'Sri Lanka',
    'Dominican Republic',
    'Indonesia',
    'Honduras',
    'Mozambique',
    'Peru',
    'Philippines',
    'India',
    'Vietnam',
    'Thailand',
    'USA',
    'World'
]

percentage2013 = [
    0.02736294,
    0.117160272,
    0.015815952 ,
    0.018831589,
    0.020409103 ,
    0.00000000285,
    0.018876854,
    0.018998639,
    0.117221146,
    0.067991687,
    0.496110972,
    0.019309486,
    0.026880553,
    0.03503080414999993
]

I want to make a stacked bar plot so that there's a stacked bar for 2012 and another for 2013.

How can I go about this since the countries in 2012 and 2013 are different?

解决方案

IIUC, you can create a Pandas dataframe and use its plot function:

import pandas as pd
df = pd.concat([pd.DataFrame({2012:percentage2012}, index=countries2012),
                pd.DataFrame({2013:percentage2013}, index=countries2013)],
               axis=1, sort=False)

df.T.plot.bar(stacked=True, figsize=(12,6))

Output:

这篇关于Seaborn 中的堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 21:01