一、概述
Matplotlib
是python中的一個包,主要用于繪制2D圖形(當然也可以繪制3D懒鉴,但是需要額外安裝支持的工具包)。在數(shù)據(jù)分析領(lǐng)域它有很大的地位隶债,而且具有豐富的擴展,能實現(xiàn)更強大的功能跑筝。能夠生成各種格式的圖形(諸如折線圖死讹,散點圖,直方圖等等)曲梗,界面可交互(可以利用鼠標對生成圖形進行點擊操作)赞警,同時該2D圖形庫跨平臺,即既可以在Python腳本中編碼操作虏两,也可以在Jupyter Notebook
中使用愧旦,以及其他平臺都可以很方便的使用Matplotlib圖形庫。
實例:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x = np.linspace(0,2*np.pi,100)
y = np.sin(x)
plt.plot(x,y)
結(jié)果:
運行結(jié)果
二定罢、Matplotlib簡單繪圖(plot)
實例:
a = [1, 2, 3]
b = [4, 5, 6]
plt.plot(a, b)
#將線條設(shè)置成--
plt.plot(a, b, '--')
運行結(jié)果:
第一結(jié)果
--線條結(jié)果
實例:
- 在一個坐標軸畫兩個圖
a = [1, 2, 3]
b = [4, 5, 6]
plt.plot(a, b)
c = [10,8,6]
d = [1,8,3]
#第一個圖的線條為紅色--
#第二個圖的為藍色*
plt.plot(a,b, 'r--', c,d, 'b*')
運行結(jié)果:
運行結(jié)果
實例:
- X/Y軸label設(shè)置
t = np.arange(0.0, 2.0, 0.1)
s = np.sin(t*np.pi)
#畫圖
plt.plot(t,s,'r--',label='aaaa')
plt.plot(t*2, s, 'b--', label='bbbb')
plt.xlabel('this is x')
plt.ylabel('this is y')
plt.title('this is a demo')
plt.legend()#顯示樣式
運行結(jié)果:
運行結(jié)果
三笤虫、Matplotlib繪圖(Subplot)
- 多個坐標軸多個圖
實例:
x = np.linspace(0.0, 5.0)
y1 = np.sin(np.pi*x)
y2 = np.sin(np.pi*x*2)
plt.subplot(221)#表示生成2行2列,在第1個坐標軸里畫圖
plt.plot(x, y1, 'b--')#第一個圖
plt.ylabel('y1')
plt.subplot(222)#表示生成2行2列祖凫,在第2個坐標軸里畫圖
plt.plot(x, y2, 'r--')#第二個圖
plt.ylabel('y2')
plt.xlabel('x')
plt.subplot(223)#表示生成2行2列琼蚯,在第3個坐標軸里畫圖
plt.plot(x, y1, 'r*')#第三個圖
plt.subplot(224)
plt.plot(x, y1, 'b*')#第四個圖
plt.show()#顯示圖畫
運行結(jié)果:
運行結(jié)果
實例:
figure, ax = plt.subplots(2,2)
ax[0][0].plot(x, y1)#在第1坐標軸畫圖
ax[0][1].plot(x, y2)#在第2坐標軸畫圖
plt.show()
運行結(jié)果:
運行結(jié)果
四、Pandas繪圖
*Series
首先我們導(dǎo)入所有的模塊:
import numpy as np
import pandas as pd
from pandas import Series,Dataframe
import matplotlib.pyplot as plt
實例:
s1 = Series(np.random.randn(1000)).cumsum()
s2 = Series(np.random.randn(1000)).cumsum()
s1.plot(kind='line',grid=True, label='S1', title='This is Series')
s2.plot(label='S2')
plt.legend()
plt.show()
運行結(jié)果:
image.png
實例:
fig, ax = plt.subplots(2,1)
print(ax)
print('*******************')
ax[0].plot(s1)
ax[1].plot(s2)
plt.show()
運行結(jié)果:
運行結(jié)果
實例:
#使用subplots
fig, ax = plt.subplots(2,1)
s1[0:10].plot(ax=ax[0], label='S1', kind='bar')
s2.plot(ax=ax[1], label='S2')
plt.show()
運行結(jié)果:
image.png
- DataFrame
實例:
df = DataFrame(
np.random.randint(1,10,40).reshape(10,4),
columns=['A','B','C','D']
)
#柱狀圖
df.plot(kind='bar')
plt.show()
#橫柱
df.plot(kind='barh')
plt.show()
df.plot(kind='bar', stacked=True)
plt.show()
#面積圖
df.plot(kind='area')
plt.show()
運行結(jié)果:
運行結(jié)果
運行結(jié)果
五惠况、matplotlib里的直方圖和密度圖
- 直方圖
實例:
#直方圖
s = Series(np.random.randn(1000))
plt.hist(s, rwidth=0.9)
print(plt.hist(s, rwidth=0.9))
print('****************')
plt.show()
a = np.arange(10)
plt.hist(a,rwidth=0.9)
plt.show()
re = plt.hist(s, rwidth=0.9)
print(len(re))
print('****************')
print(re[0])
print('****************')
print(re[1])
print('****************')
print(re[2])
plt.show()
plt.hist(s, rwidth=0.9,bins=20, color='r')#設(shè)置為紅色
plt.show()
運行結(jié)果:
運行結(jié)果
運行結(jié)果
運行結(jié)果
- 密度圖
實例:
#將`kind`參數(shù)設(shè)置`kde`
s.plot(kind='kde')
print(s.plot(kind='kde'))
plt.show()
運行結(jié)果