Population Pyramid with Python and Seaborn(人口金字塔,包括巨蟒和海上金字塔)
本文介绍了人口金字塔,包括巨蟒和海上金字塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个按性别分组的人口金字塔。不幸的是,我不能让它工作。情节只是一幅白色的图画,轴线似乎在某种程度上颠倒了过来。也许有人能帮我,谢谢。
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)
输出:
这篇关于人口金字塔,包括巨蟒和海上金字塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:人口金字塔,包括巨蟒和海上金字塔
基础教程推荐
猜你喜欢
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
