stackplot函數(shù)語(yǔ)法及參數(shù)含義
?stackplot(x,*args,**kargs)?
x指定面積圖的x軸數(shù)據(jù)
?*args為可變參數(shù),可以接受任意多的y軸數(shù)據(jù),即各個(gè)拆分的數(shù)據(jù)對(duì)象?
**kargs為關(guān)鍵字參數(shù),可以通過(guò)傳遞其他參數(shù)來(lái)修飾面積圖,如標(biāo)簽、顏色 可用的關(guān)鍵字參數(shù): labels:以列表的形式傳遞每一塊面積圖包含的標(biāo)簽,通過(guò)圖例展現(xiàn) colors:設(shè)置不同的顏色填充面積圖?
以我國(guó)2017年的物流運(yùn)輸量為例,來(lái)對(duì)比繪制折線圖和面積圖。這里將物流運(yùn)輸量拆分成公路運(yùn)輸、鐵路運(yùn)輸和水路運(yùn)輸,繪圖的對(duì)比代碼見(jiàn)下方所示:
折線圖
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rcParams['font.sans-serif']=['SimHei'] #用來(lái)正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus']=False #用來(lái)正常顯示負(fù)號(hào)
#read data
transport = pd.read_excel('transport.xls')
#head of row
#print(transport.head())
N = np.arange(transport.shape[1]-1)
#plot zhexian
labels = transport.Index
channel = transport.columns[1:]
for i in range (transport.shape[0]):
? ? plt.plot(N,#x
? ? ? ? ? ? transport.loc[i,'Jan':'Aug'],#y
? ? ? ? ? ? label = labels[i],#add label
? ? ? ? ? ? marker = 'o',
? ? ? ? ? ? linewidth = 2
? ? ? ? ? ? )
plt.title("2017年各運(yùn)輸渠道的運(yùn)輸量")
plt.ylabel("運(yùn)輸量(萬(wàn)噸)")
plt.xticks(N,channel)
plt.tick_params(top = 'off',right = 'off')
plt.legend(loc = 'best')
plt.show()
這就是繪制分組的折線圖思想,雖然折線圖能夠反映各個(gè)渠道的運(yùn)輸量隨月份的波動(dòng)趨勢(shì),但無(wú)法觀察到1月份到8月份的各自總量。接下來(lái)我們看看面積圖的展現(xiàn)。
面積圖
x = N
y1 = transport.loc[0,'Jan':'Aug'].astype('int')
y2 = transport.loc[1,'Jan':'Aug'].astype('int')
y3 = transport.loc[2,'Jan':'Aug'].astype('int')
colors = ["red","blue","pink"]
plt.stackplot(x,
? ? ? ? ? ? ? ? ? ? y1,y2,y3,
? ? ? ? ? ? ? ? ? ? labels = labels,
? ? ? ? ? ? ? ? ? ? colors = colors
? ? ? ? ? ? ? ? ? ? ?)
plt.title("2017年交通各渠道的運(yùn)輸量")
plt.ylabel("累計(jì)運(yùn)輸量(萬(wàn)噸)")
plt.xticks(N,channel)
plt.tick_params(top = 'off',right = 'off')
plt.legend(loc = 'upper left')
plt.show()
一個(gè)stackplot函數(shù)就能解決問(wèn)題,而且具有很強(qiáng)的定制化。從上面的面積圖就可以清晰的發(fā)現(xiàn)兩個(gè)方面的信息,一個(gè)是各渠道運(yùn)輸量的趨勢(shì),另一個(gè)是則可以看見(jiàn)各月份的總量趨勢(shì)。所以,我們?cè)诳梢暬倪^(guò)程中要盡可能的為閱讀者輸出簡(jiǎn)單而信息量豐富的圖形。