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()

 

圖像如下:

可以看出,大的趨勢是:年輕人口比重在逐年減少,老年人口比重則逐年增高。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM