今天是讀《pyhton數(shù)據(jù)分析基礎(chǔ)》的第14天,今天讀書筆記的內(nèi)容為使用matplotlib模塊繪制常用的統(tǒng)計(jì)圖。
模塊概括
matplotlib 是最基礎(chǔ)的繪圖模塊,pandas和seaborn的繪圖功能的使用依賴于matplotlib。
條形圖
#繪制柱形圖
from matplotlib import pyplot as plt
#繪圖數(shù)據(jù)
x=["a","c","d","e","b"]
y=[11.5,18.6,17.5,14.3,10.8]
#創(chuàng)建基礎(chǔ)圖
fig=plt.figure()
#在基礎(chǔ)圖上僅繪制一個(gè)圖,括號中的三個(gè)參數(shù)代表基礎(chǔ)圖中的統(tǒng)計(jì)圖布局,參數(shù)一次代表:圖的行數(shù)量、圖的列數(shù)量、第幾個(gè)圖。本例中,為1行1列,第一個(gè)圖
bar1=fig.add_subplot(1,1,1)
#繪制柱形圖,align表示條形與標(biāo)簽中間對齊。
bar1.bar(x,y,align='center',color="grey")
#設(shè)置基礎(chǔ)圖形中某個(gè)統(tǒng)計(jì)圖(這里指柱形圖)的坐標(biāo)軸位置
bar1.xaxis.set_ticks_position("bottom")
bar1.yaxis.set_ticks_position("left")
#設(shè)置x、y軸標(biāo)簽
plt.xlabel("variable x")
plt.ylabel("variable y")
#設(shè)置統(tǒng)計(jì)圖標(biāo)題
plt.title("sample_bar char")
#顯示統(tǒng)計(jì)圖
plt.show()
統(tǒng)計(jì)圖如下:

柱形圖.png
直方圖
#繪制直方圖
from matplotlib import pyplot as plt
import numpy as np
#設(shè)置數(shù)據(jù):兩組正態(tài)分布的數(shù)據(jù)
mu1, mu2, sigma = 100, 130, 15
x1 = mu1 + sigma*np.random.randn(10000)
x2 = mu2 + sigma*np.random.randn(10000)
#繪制基礎(chǔ)圖
fig=plt.figure()
hist1=fig.add_subplot(1,1,1)
#繪制直方圖
#bins=50 表示每個(gè)變量的 值應(yīng)該被分成 50 份。normed=False 表示直方圖顯示的是頻率分布
hist1.hist(x1,bins=50,normed=False)
hist1.hist(x2,bins=50,normed=False,alpha=0.5)
#確定坐標(biāo)軸位置
hist1.xaxis.set_ticks_position("bottom")
hist1.yaxis.set_ticks_position("left")
#設(shè)置坐標(biāo)軸標(biāo)簽
plt.xlabel("x")
plt.ylabel("frequencyof x1,x2")
#設(shè)置標(biāo)題
plt.title("sample_histagram")
#顯示圖形
plt.show()
統(tǒng)計(jì)圖如下:

直方圖.png
折線圖
#繪制折線圖
from matplotlib import pyplot as plt
#設(shè)置繪圖數(shù)據(jù)
x=[1,2,3,4,5]
y=[10.2,13.0,15.1,15.2,16.2]
#繪制基礎(chǔ)圖
fig=plt.figure()
plot1=fig.add_subplot(1,1,1)
#繪制折線圖
plot1.plot(x,y,marker=r".")
#去頂坐標(biāo)軸位置
plot1.xaxis.set_ticks_position("bottom")
plot1.yaxis.set_ticks_position("left")
#確定坐標(biāo)軸標(biāo)簽
plt.xlabel("month")
plt.ylabel("score")
#圖標(biāo)題
plt.title("sample_plot")
#顯示圖形
plt.show()
統(tǒng)計(jì)圖如下:

折線圖.png
散點(diǎn)圖
#繪制散點(diǎn)圖
from matplotlib import pyplot as plt
import numpy as np
#準(zhǔn)備繪圖數(shù)據(jù)
x=np.random.randn(50).cumsum()
y=np.random.randn(50).cumsum()
#繪制基礎(chǔ)圖
fig=plt.figure()
scatter1=fig.add_subplot(1,1,1)
#繪制散點(diǎn)圖
scatter1.scatter(x,y)
#確定坐標(biāo)軸位置
scatter1.xaxis.set_ticks_position('bottom')
scatter1.yaxis.set_ticks_position('left')
#設(shè)置坐標(biāo)軸標(biāo)簽
plt.xlabel("variable x")
plt.ylabel("variable y")
#設(shè)置圖表標(biāo)題
plt.title("sample_scatter")
#顯示圖形
plt.show()
統(tǒng)計(jì)圖如下:

散點(diǎn)圖.png
箱線圖
#繪制箱線圖
from matplotlib import pyplot as plt
import numpy as np
#準(zhǔn)備繪圖數(shù)據(jù)
normal = np.random.normal(loc=0.0, scale=1.0, size=100)
lognormal = np.random.lognormal(mean=0.0, sigma=1.0, size=100)
data = [normal,lognormal]
dataLabel=["normal","lognormal"]
#繪制基本圖
fig=plt.figure()
boxPlot1=fig.add_subplot(1,1,1)
#繪制箱線圖
boxPlot1.boxplot(data,labels=dataLabel)
#確定坐標(biāo)周位置
boxPlot1.xaxis.set_ticks_position("bottom")
boxPlot1.yaxis.set_ticks_position("left")
#設(shè)定坐標(biāo)軸標(biāo)簽
plt.xlabel("catagory")
plt.ylabel("value")
#設(shè)定標(biāo)題
plt.title("sample_boxPlot")
#顯示統(tǒng)計(jì)圖
plt.show()
統(tǒng)計(jì)圖如下:

箱線圖.png