人口金字塔,包括巨蟒和海上金字塔 [英] Population Pyramid with Python and Seaborn

查看:26
本文介绍了人口金字塔,包括巨蟒和海上金字塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个按性别分组的人口金字塔。不幸的是,我不能让它工作。情节只是一幅白色的图画,轴线似乎在某种程度上颠倒了过来。也许有人能帮我,谢谢。

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

# I read this testdata from a csv file
testdata = pd.DataFrame({'age': [20, 20, 21, 21, 22, 22, 23, 23],
                'gender': ["male", "female", "male", "female", "male", "female", "male", "female"],
                'count': [10, -12, 13, -10, 16, -14, 17, -16]});


plt.figure(figsize=(13, 10), dpi=80)
group_col = 'gender'
order_of_bars = testdata['age'].unique()[::-1]
colors = [plt.cm.Spectral(i / float(len(testdata[group_col].unique()) - 1)) for i in range(len(testdata[group_col].unique()))]


for c, group in zip(colors, testdata[group_col].unique()):
    barplot = sns.barplot(x='count', y='age', data=testdata.loc[testdata[group_col] == group, :], order=order_of_bars, color=c, label=group)

plt.xlabel("Counts")
plt.ylabel("Age")
plt.yticks(fontsize=12)
plt.title("Pyramide", fontsize=22)
plt.legend()
plt.show()

推荐答案

如果您要查找this population pyramid,我们试试:

sns.barplot(data=testdata, x='count',y='age',
            hue='gender',orient='horizontal', 
            dodge=False)

输出:

这篇关于人口金字塔,包括巨蟒和海上金字塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆