如何使用python

我正在尝试使用seaborn从熊猫df建立人口金字塔。问题是某些数据没有显示。从我创建的绘图中可以看到,缺少一些数据。 Y轴刻度是21,而df的年龄是21,所以为什么不匹配?我想念什么?

如何使用python

这是我写的代码:

 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+','0-4']

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

bar_plot = sns.barplot(x='Female',order=Ageclass)

bar_plot.set(xlabel="Population (hundreds of millions)",ylabel="Age-Group",title = "Population Pyramid")
DJJ980620 回答:如何使用python

正如JohanC解释的那样,数据不丢失,与其他条形图相比,它很小。 另一个因素是,您似乎在每个小节周围都有一个白色边框,从而将顶部的小条隐藏起来。尝试将lw=0放入对barplot的呼叫中。这就是我得到的:

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

enter image description here

本文链接:https://www.f2er.com/2536721.html

大家都在问