我正在尝试创建一个按性别分组的人口金字塔。不幸的是,我无法让它发挥作用。情节只是一张白色的图片,轴似乎以某种方式颠倒了。也许有人可以帮助我,谢谢。
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()
MMMHUHU
相关分类