簡體   English   中英

如何用python構建人口金字塔

[英]How to build a population pyramid with python

我正在嘗試使用 seaborn 從熊貓 df 構建人口金字塔。 問題是一些數據沒有顯示。 從我創建的圖中可以看出,有一些缺失的數據。 Y 軸刻度為 21,df 的年齡等級為 21,為什么它們不匹配? 我錯過了什么?

在此處輸入圖片說明

這是我寫的代碼:

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

 df = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'], 
                    'Male': [-49228000, -61283000, -64391000, -52437000, -42955000, -44667000, -31570000, -23887000, -22390000, -20971000, -17685000, -15450000, -13932000, -11020000, -7611000, -4653000, -1952000, -625000, -116000, -14000, -1000], 
                    'Female': [52367000, 64959000, 67161000, 55388000, 45448000, 47129000, 33436000, 26710000, 25627000, 23612000, 20075000, 16368000, 14220000, 10125000, 5984000, 3131000, 1151000, 312000, 49000, 4000, 0]})


AgeClass = ['100+','95-99','90-94','85-89','80-84','75-79','70-74','65-69','60-64','55-59','50-54','45-49','40-44','35-39','30-34','25-29','20-24','15-19','10-14','5-9','0-4']

bar_plot = sns.barplot(x='Male', y='Age', data=df, order=AgeClass)

bar_plot = sns.barplot(x='Female', y='Age', data=df, order=AgeClass)

bar_plot.set(xlabel="Population (hundreds of millions)", ylabel="Age-Group", title = "Population Pyramid")

正如 JohanC 所解釋的,數據並沒有丟失,只是與其他條形相比非常小。 另一個因素是您的每個條形周圍似乎都有一個白色邊框,它隱藏了頂部的非常小的條形。 嘗試將lw=0放在對barplot的調用中。 這就是我得到的:

bar_plot = sns.barplot(x='Male', y='Age', data=df, order=AgeClass, lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=df, order=AgeClass, lw=0)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM