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)
输出:
这篇关于人口金字塔,包括巨蟒和海上金字塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:人口金字塔,包括巨蟒和海上金字塔


猜你喜欢
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 沿轴计算直方图 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01