Matplotlib学习---用matplotlib画面积图(area chart)

这里利用Nathan Yau所著的《鲜活的数据:数据可视化指南》一书中的数据,学习画图。

 

数据地址:http://book.flowingdata.com/ch05/data/us-population-by-age.xls

 

准备工作:先导入matplotlib和pandas,用pandas读取excel文件,然后创建一个图像和一个坐标轴

import pandas as pd
from matplotlib import pyplot as plt
population=pd.read_excel(r"http://book.flowingdata.com/ch05/data/us-population-by-age.xls")
fig,ax=plt.subplots()

 

 先来看一看这个数据文件:

                                                    Under 5  5 to 19  \
1860                                                   15.4     35.8   
1870                                                   14.3     35.4   
1880                                                   13.8     34.3   
1890                                                   12.2     33.9   
1900                                                   12.1     32.3   
1910                                                   11.6     30.4   
1920                                                   10.9     29.8   
1930                                                    9.3     29.5   
1940                                                    8.0     26.4   
1950                                                   10.7     23.2   
1960                                                   11.3     27.1   
1970                                                    8.4     29.5   
1980                                                    7.2     24.8   
1990                                                    7.6     21.3   
2000                                                    6.8     21.8   
2005                                                    6.8     20.7   
NaN                                                    -8.6    -15.1   
NaN                                                     NaN      NaN   
Read more: Population Distribution by Age, Race...      NaN      NaN   

                                                    20 to 44  45 to 64   65+  
1860                                                    35.7      10.4   2.7  
1870                                                    35.4      11.9   3.0  
1880                                                    35.9      12.6   3.4  
1890                                                    36.9      13.1   3.9  
1900                                                    37.7      13.7   4.1  
1910                                                    39.0      14.6   4.3  
1920                                                    38.4      16.1   4.7  
1930                                                    38.3      17.4   5.4  
1940                                                    38.9      19.8   6.8  
1950                                                    37.6      20.3   8.1  
1960                                                    32.2      20.1   9.2  
1970                                                    31.7      20.6   9.8  
1980                                                    37.1      19.6  11.3  
1990                                                    40.1      18.6  12.5  
2000                                                    37.0      22.0  12.4  
2005                                                    35.4      24.6  12.4  
NaN                                                     -0.3      14.2   9.7  
NaN                                                      NaN       NaN   NaN  
Read more: Population Distribution by Age, Race...       NaN       NaN   NaN 

这个文件记录的是1860年-2005年美国各年龄段人口占总人口的百分比。由于文件里有NaN字样,因此先把有效数据提取出来。然后把各年龄段的人口数据堆叠起来,画一个面积图。

 

面积图: ax.stackplot(x,y1,y2,y3...)

 

代码如下:

import pandas as pd
from matplotlib import pyplot as plt
population=pd.read_excel(r"http://book.flowingdata.com/ch05/data/us-population-by-age.xls")
fig,ax=plt.subplots(figsize=(7,5))

p1=population.iloc[0:16] #提取有效数据
year=p1.index.astype(int) #提取年份,并转换为整数类型

v1=p1["Under 5"].values #提取5岁以下的数据
v2=p1["5 to 19"].values #提取5-19岁的数据
v3=p1["20 to 44"].values #提取20-44岁的数据
v4=p1["45 to 64"].values #提取45-64岁的数据
v5=p1["65+"].values #提取65岁以上的数据

#设置y轴刻度值的一个helper function
def make_yticks(where):
    ytick=[]
    sum=0
    for i in where:
        sum+=i
        ytick.append(sum)
    return ytick

ax.stackplot(year,v1,v2,v3,v4,v5)
ax.set(xlim=(1860,2005),ylim=(0,100),xlabel="Year",ylabel="Population %")
ax1=ax.twinx() #设置双y轴,共享x轴
ax.set_yticks(make_yticks(p1.loc[1860])) #设置第一个y轴刻度值
ax1.set_yticks(make_yticks(p1.loc[2005])) #设置第二个y轴刻度值
diff=[i-j for i,j in zip(p1.loc[2005],p1.loc[1860])] #计算2005年减去1860年的差值
for i,j,z in zip(make_yticks(p1.loc[2005]), p1.columns,diff): #设置文字注释
    ax.text(x=1980,y=i-6,s=j)
    ax.text(x=2020,y=i-6,s=z,fontsize=14,color="b")

plt.show()

 

图像如下:

可以看出,大的趋势是:年轻人口比重在逐年减少,老年人口比重则逐年增高。

 

posted @ 2018-08-17 16:53  HuZihu  阅读(4547)  评论(0编辑  收藏  举报