我正在嘗試創建一個按性別分組的人口金字塔。不幸的是,我無法讓它發揮作用。情節只是一張白色的圖片,軸似乎以某種方式顛倒了。也許有人可以幫助我,謝謝。import pandas as pdimport seaborn as snsimport matplotlib.pyplot as plt# I read this testdata from a csv filetestdata = 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()
1 回答

MMMHUHU
TA貢獻1834條經驗 獲得超8個贊
如果您正在尋找這個人口金字塔,讓我們嘗試:
sns.barplot(data=testdata,?x='count',y='age', ????????????hue='gender',orient='horizontal',? ????????????dodge=False)
輸出:
添加回答
舉報
0/150
提交
取消