Matplotlib学习---用matplotlib画柱形图,条形图,堆积柱形图(bar chart)
這里利用Nathan Yau所著的《鮮活的數(shù)據(jù):數(shù)據(jù)可視化指南》一書中的數(shù)據(jù),學習畫圖。
數(shù)據(jù)地址:http://datasets.flowingdata.com/hot-dog-contest-winners.csv (用于普通柱形圖)
http://datasets.flowingdata.com/hot-dog-places.csv (用于堆積柱形圖和橫向柱形圖)
準備工作:先導入matplotlib和pandas,用pandas讀取csv文件,然后創(chuàng)建一個圖像和一個坐標軸
import pandas as pd hot_dog=pd.read_csv(r"http://datasets.flowingdata.com/hot-dog-contest-winners.csv") from matplotlib import pyplot as plt fig,ax=plt.subplots()
讓我們先看看第一個數(shù)據(jù)文件的前5行:
Year Winner Dogs eaten Country New record 0 1980 Paul Siederman & Joe Baldini 9.1 United States 0 1 1981 Thomas DeBerry 11.0 United States 0 2 1982 Steven Abrams 11.0 United States 0 3 1983 Luis Llamas 19.5 Mexico 0 4 1984 Birgit Felden 9.5 Germany 0
這個數(shù)據(jù)展示的是從1980年開始,每年吃熱狗大賽的冠軍,冠軍吃掉熱狗的數(shù)量,冠軍的國籍,以及是否創(chuàng)新紀錄(0表示沒有破紀錄,1表示創(chuàng)造了新紀錄)。
讓我們來畫一個各年份冠軍吃掉熱狗數(shù)量的柱形圖。
A.普通柱形圖
ax.bar(x,y)
import pandas as pd
hot_dog=pd.read_csv(r"http://datasets.flowingdata.com/hot-dog-contest-winners.csv")
from matplotlib import pyplot as plt
fig,ax=plt.subplots()
ax.bar(hot_dog["Year"],hot_dog["Dogs eaten"])
ax.set_xlabel("Year") #設(shè)置x軸標簽
ax.set_ylabel("Dogs Eaten") #設(shè)置y軸標簽
ax.set_title("Hotdog game scores 1980-2010") #設(shè)置標題
ax.set_xlim(1979,2011) #設(shè)置x軸數(shù)據(jù)限值
plt.show() #顯示圖像
圖像如下:
?
如果我們需要把創(chuàng)造新紀錄的年份區(qū)分開來呢?如何用不同的顏色進行表示?這時就需要寫一個helper function,把各個年份應顯示的顏色放入一個列表中。
import pandas as pd
hot_dog=pd.read_csv(r"http://datasets.flowingdata.com/hot-dog-contest-winners.csv")
from matplotlib import pyplot as plt
fig,ax=plt.subplots()
def hotdog_color():
"創(chuàng)新紀錄的為紅色,其余為藍色"
list=[]
for i in hot_dog["New record"]:
if i==1:
list.append("red")
else:
list.append("blue")
return list
ax.bar(hot_dog["Year"],hot_dog["Dogs eaten"],color=hotdog_color())
ax.set_xlabel("Year") #設(shè)置x軸標簽
ax.set_ylabel("Dogs Eaten") #設(shè)置y軸標簽
ax.set_title("Hotdog game scores 1980-2010") #設(shè)置標題
ax.set_xlim(1979,2011) #設(shè)置x軸數(shù)據(jù)限值
plt.show() #顯示圖像
此時圖像如下:
這個顏色有點丑哈。。。對于配色問題,大家可以多嘗試一下。
讓我們再來看看另一個數(shù)據(jù)文件:
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 0 25 50.0 50.5 44.5 53.5 49 54 66 59 68.0 54 1 24 31.0 26.0 30.5 38.0 37 52 63 59 64.5 43 2 22 23.5 25.5 29.5 32.0 32 37 49 42 55.0 37
這個文件很簡短,展示的就是各年份冠亞季軍所吃熱狗的數(shù)量。
那么如何把各年份冠亞季軍所吃熱狗的數(shù)量用柱形圖畫出來呢?這時就要用到堆積柱形圖了。
B.堆積柱形圖
思路:因為需要把冠亞季軍所吃熱狗的數(shù)量都展現(xiàn)出來,因此這個柱形圖每個柱子的高度應該是冠亞季軍所吃熱狗數(shù)量的總和。如果在最上層展現(xiàn)冠軍,那么先把冠亞季軍所吃熱狗數(shù)量的總和畫出來,第二層是亞軍,再畫亞軍所吃熱狗的數(shù)量+季軍所吃熱狗的數(shù)量,用不同的顏色表示,這樣第一次畫的柱子下面就被第二次畫的柱子所覆蓋,第三層同理。
import pandas as pd hot_dog=pd.read_csv(r"http://datasets.flowingdata.com/hot-dog-places.csv") from matplotlib import pyplot as plt fig,ax=plt.subplots() year=[int(i) for i in hot_dog.columns] #年份從header中提取 value=hot_dog.T.values #將冠亞季軍所吃熱狗的數(shù)量轉(zhuǎn)化成matrix,也就是[[25,24,22],[50.0,31.0,23.5],...] v1=[i[0]+i[1]+i[2] for i in value] #第一次畫的柱形圖y值為冠亞季軍所吃熱狗數(shù)量的總和 v2=[i[1]+i[2] for i in value] #第二次畫的柱形圖y值為亞軍所吃熱狗的數(shù)量+季軍所吃熱狗的數(shù)量 v3=[i[2] for i in value] #第三次畫的柱形圖y值為季軍所吃熱狗的數(shù)量 ax.bar(year,v1,color="green") ax.bar(year,v2,color="red") ax.bar(year,v3,color="blue") ax.set(xlabel="Year",title="Hotdog game scores 2000-2010") ax.text(1998,184,"(HDB)") #設(shè)置文字 ax.legend(["first place","second place","third place"]) #設(shè)置圖例 plt.show()
圖像如下:
C.橫向柱形圖(條形圖)
把ax.bar( )換成ax.barh( )即可
(h是horizontal的意思)
import pandas as pd hot_dog=pd.read_csv(r"http://datasets.flowingdata.com/hot-dog-places.csv") from matplotlib import pyplot as plt fig,ax=plt.subplots() year=[int(i) for i in hot_dog.columns] #年份從header中提取 value=hot_dog.T.values #將冠亞季軍所吃熱狗的數(shù)量轉(zhuǎn)化成matrix,也就是[[25,24,22],[50.0,31.0,23.5],...] v1=[i[0]+i[1]+i[2] for i in value] #第一次畫的柱形圖y值為冠亞季軍所吃熱狗數(shù)量的總和 v2=[i[1]+i[2] for i in value] #第二次畫的柱形圖y值為亞軍所吃熱狗的數(shù)量+季軍所吃熱狗的數(shù)量 v3=[i[2] for i in value] #第三次畫的柱形圖y值為季軍所吃熱狗的數(shù)量 ax.barh(year,v1,color="green") ax.barh(year,v2,color="red") ax.barh(year,v3,color="blue") ax.set(ylabel="Year",title="Hotdog game scores 2000-2010") ax.text(184,1998.3,"(HDB)") #設(shè)置文字 ax.legend(["first place","second place","third place"]) #設(shè)置圖例 plt.show()
圖像如下:
總結(jié)
以上是生活随笔為你收集整理的Matplotlib学习---用matplotlib画柱形图,条形图,堆积柱形图(bar chart)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对话jQuery之父John Resig
- 下一篇: 在SAP Cloud Platform上