如何在子图中绘制多个Seaborn Barplots?

马可·马里亚尼(Marco Mariani)

我想创建一个包含三个海底条形图的子图。我已经创建了三个人口金字塔的条形图,但是我不知道如何将它们作为子图组合在一起。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

'''1980'''

Population1980 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'], 
                               'Male': [-49228000, -61283000, -64391000, -52437000, -42955000, -44667000, -31570000, -23887000, -22390000, -20971000, -17685000, -15450000, -13932000, -11020000, -7611000, -4653000, -1952000, -625000, -116000, -14000, -1000], 
                               'Female': [52367000, 64959000, 67161000, 55388000, 45448000, 47129000, 33436000, 26710000, 25627000, 23612000, 20075000, 16368000, 14220000, 10125000, 5984000, 3131000, 1151000, 312000, 49000, 4000, 0]})

AgeClass = ['100+','95-99','90-94','85-89','80-84','75-79','70-74','65-69','60-64','55-59','50-54','45-49','40-44','35-39','30-34','25-29','20-24','15-19','10-14','5-9','0-4']
labels = ['80M', '60M', '40M', '20M', '0', '20M', '40M', '60M']

bar_plot = sns.barplot(x='Male', y='Age', data=Population1980, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population1980, order=AgeClass, palette='PuBu', lw=0)

bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "1980")
bar_plot.set_xticklabels(labels)

'''2020'''

Population2020 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
                               'Male': [-39476000, -40415000, -38913000, -38239000, -40884000, -46466000, -62296000, -48746000, -46985000, -58664000, -61097000, -48782000, -38597000, -37623000, -23525000, -14337000, -9298000, -4739000, -1574000, -359000, -62000],
                               'Female': [44456000, 46320000, 45350000, 44103000, 46274000, 51523000, 66443000, 51346000, 49289000, 61173000, 62348000, 49958000, 38917000, 36527000, 21425000, 12207000, 6884000, 2843000, 731000, 116000, 13000]})

bar_plot = sns.barplot(x='Male', y='Age', data=Population2020, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population2020, order=AgeClass, palette='PuBu', lw=0)

bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2020")
bar_plot.set_xticklabels(labels)

'''2050'''

Population2050 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
                               'Male': [-31222000, -32130000, -32532000, -33006000, -33639000, -35628000, -38650000, -39462000, -37812000, -37015000, -39486000, -44586000, -58817000, -44365000, -39900000, -43830000, -36255000, -19327000, -7942000, -2883000, -497000],
                               'Female': [33392000, 34351000, 34764000, 35250000, 36576000, 39416000, 43473000, 45150000, 43954000, 42485000, 44282000, 48656000, 61036000, 44548000, 38445000, 39264000, 28884000, 13627000, 4539000, 1207000, 123000]})

bar_plot = sns.barplot(x='Male', y='Age', data=Population2050, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population2050, order=AgeClass, palette='PuBu', lw=0)

bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2050")
bar_plot.set_xticklabels(labels)

这是我获得的三个独立的条形图。 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

    import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

fig, axes = plt.subplots(2, 2)

Population1980 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'], 
                       'Male': [-49228000, -61283000, -64391000, -52437000, -42955000, -44667000, -31570000, -23887000, -22390000, -20971000, -17685000, -15450000, -13932000, -11020000, -7611000, -4653000, -1952000, -625000, -116000, -14000, -1000], 
                       'Female': [52367000, 64959000, 67161000, 55388000, 45448000, 47129000, 33436000, 26710000, 25627000, 23612000, 20075000, 16368000, 14220000, 10125000, 5984000, 3131000, 1151000, 312000, 49000, 4000, 0]})

Population2020 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
                               'Male': [-39476000, -40415000, -38913000, -38239000, -40884000, -46466000, -62296000, -48746000, -46985000, -58664000, -61097000, -48782000, -38597000, -37623000, -23525000, -14337000, -9298000, -4739000, -1574000, -359000, -62000],
                               'Female': [44456000, 46320000, 45350000, 44103000, 46274000, 51523000, 66443000, 51346000, 49289000, 61173000, 62348000, 49958000, 38917000, 36527000, 21425000, 12207000, 6884000, 2843000, 731000, 116000, 13000]})

Population2050 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
                               'Male': [-31222000, -32130000, -32532000, -33006000, -33639000, -35628000, -38650000, -39462000, -37812000, -37015000, -39486000, -44586000, -58817000, -44365000, -39900000, -43830000, -36255000, -19327000, -7942000, -2883000, -497000],
                               'Female': [33392000, 34351000, 34764000, 35250000, 36576000, 39416000, 43473000, 45150000, 43954000, 42485000, 44282000, 48656000, 61036000, 44548000, 38445000, 39264000, 28884000, 13627000, 4539000, 1207000, 123000]})

AgeClass = ['100+','95-99','90-94','85-89','80-84','75-79','70-74','65-69','60-64','55-59','50-54','45-49','40-44','35-39','30-34','25-29','20-24','15-19','10-14','5-9','0-4']
labels = ['80M', '60M', '40M', '20M', '0', '20M', '40M', '60M']

bar_plot = sns.barplot(x='Male', y='Age', data=Population1980, order=AgeClass, orient='h', ax=axes[0], palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population1980, order=AgeClass, orient='h', ax=axes[0], palette='PuBu', lw=0)
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "1980")

bar_plot = sns.barplot(x='Male', y='Age', data=Population2020, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population2020, order=AgeClass, palette='PuBu', lw=0)
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2020")

bar_plot = sns.barplot(x='Male', y='Age', data=Population2050, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population2050, order=AgeClass, palette='PuBu', lw=0)
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2050")

这是我所做的更改,我只为第一个小节尝试了ax = axes [0]。

杰桑托索

展平轴更容易:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

fig, axes = plt.subplots(2, 2)
axes = axes.flatten()

Population1980 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'], 
                       'Male': [-49228000, -61283000, -64391000, -52437000, -42955000, -44667000, -31570000, -23887000, -22390000, -20971000, -17685000, -15450000, -13932000, -11020000, -7611000, -4653000, -1952000, -625000, -116000, -14000, -1000], 
                       'Female': [52367000, 64959000, 67161000, 55388000, 45448000, 47129000, 33436000, 26710000, 25627000, 23612000, 20075000, 16368000, 14220000, 10125000, 5984000, 3131000, 1151000, 312000, 49000, 4000, 0]})

Population2020 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
                               'Male': [-39476000, -40415000, -38913000, -38239000, -40884000, -46466000, -62296000, -48746000, -46985000, -58664000, -61097000, -48782000, -38597000, -37623000, -23525000, -14337000, -9298000, -4739000, -1574000, -359000, -62000],
                               'Female': [44456000, 46320000, 45350000, 44103000, 46274000, 51523000, 66443000, 51346000, 49289000, 61173000, 62348000, 49958000, 38917000, 36527000, 21425000, 12207000, 6884000, 2843000, 731000, 116000, 13000]})

Population2050 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
                               'Male': [-31222000, -32130000, -32532000, -33006000, -33639000, -35628000, -38650000, -39462000, -37812000, -37015000, -39486000, -44586000, -58817000, -44365000, -39900000, -43830000, -36255000, -19327000, -7942000, -2883000, -497000],
                               'Female': [33392000, 34351000, 34764000, 35250000, 36576000, 39416000, 43473000, 45150000, 43954000, 42485000, 44282000, 48656000, 61036000, 44548000, 38445000, 39264000, 28884000, 13627000, 4539000, 1207000, 123000]})

AgeClass = ['100+','95-99','90-94','85-89','80-84','75-79','70-74','65-69','60-64','55-59','50-54','45-49','40-44','35-39','30-34','25-29','20-24','15-19','10-14','5-9','0-4']
labels = ['80M', '60M', '40M', '20M', '0', '20M', '40M', '60M']

bar_plot = sns.barplot(x='Male', y='Age', data=Population1980, order=AgeClass, orient='h', ax=axes[0], palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population1980, order=AgeClass, orient='h', ax=axes[0], palette='PuBu', lw=0)
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "1980")

bar_plot = sns.barplot(x='Male', y='Age', data=Population2020, order=AgeClass, palette='OrRd', lw=0, ax=axes[1])
bar_plot = sns.barplot(x='Female', y='Age', data=Population2020, order=AgeClass, palette='PuBu', lw=0, ax=axes[1])
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2020")

bar_plot = sns.barplot(x='Male', y='Age', data=Population2050, order=AgeClass, palette='OrRd', lw=0, ax=axes[2])
bar_plot = sns.barplot(x='Female', y='Age', data=Population2050, order=AgeClass, palette='PuBu', lw=0, ax=axes[2])
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2050")

展平的原因是axes2 x 2的数组,因此您必须使用2个索引来获得所需的斧头。使用会更容易axes.flatten(),因为将数组从2 x 2转换为1 x 4维,因此,您只需要一个索引。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为子图调整seaborn barplots中条的不同厚度

将Pandas crosstab与Seaborn堆叠式Barplots结合使用

将来自Seaborn的Barplots排列成阵列

将barplots添加到partykit绘制树的内部节点中的圆中

seaborn displot()未在定义的子图中绘制

在 Seaborn 直方图中绘制多个分布

我如何在Seaborn的地块图中添加多个标记?

如何在与Seaborn相同的图上绘制多个直方图

如何在子图中以绘图方式绘制矩形?

如何在不同的子图中绘制对,侧面有差异

如何在一个图中绘制多个numpy数组?

Python Matplotlib如何在同一图中绘制多个图形

如何在诺基亚地图中绘制多个图层容器?

如何在一个图中绘制多个线图

如何在色度图中绘制多个 RGB 坐标

如何在Seaborn的对图中绘制右轴和上轴并删除左轴和底轴?

如何在Pandas和Seaborn散点图中使用坡度绘制线?

Seaborn绘图-在一个图中绘制多个绘图

使用Seaborn在一个图中绘制多个不同的图

如何使用Seaborn连续绘制多个图形

如何将每个绘制列的图例标签添加到多个散点图子图中?

如何在一个窗口中绘制多个seaborn`distplot`?

如何在seaborn图中显示多条线?

如何在seaborn图中显示交集标签?

如何在seaborn lineplot上绘制虚线?

如何在一个图中绘制散点图和线图作为子图?

如何在直方图中绘制具有相同标签编号的多个特征

如何在一个图中绘制多个泊松分布

如何在一个图中绘制多个折线图(覆盖/分组)