1. matplotlib
Python 的 2D繪圖庫,為Python構(gòu)建一個(gè)Matlab式的繪圖接口,通過 Matplotlib,開發(fā)者可以僅需要幾行代碼羡宙,便可以生成繪圖,直方圖掐隐,功率譜狗热,條形圖,錯(cuò)誤圖虑省,散點(diǎn)圖等
Matplotlib是最常用繪圖庫斗搞,功能上能夠滿足我們的應(yīng)用
serborn是在matplotlib的基礎(chǔ)上進(jìn)行了更高級(jí)的API封裝,是一個(gè)補(bǔ)充
Bokeh 針對(duì)web
d3.js 最高級(jí)的繪圖工具慷妙,js來寫
官方文檔
https://matplotlib.org/users/pyplot_tutorial.html
2.figure
figure可以理解為畫布
如果不創(chuàng)建figure對(duì)象僻焚,matplotlib自動(dòng)創(chuàng)建一個(gè)figure對(duì)象
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure()
print(fig)
###############運(yùn)行結(jié)果#################
Figure(432x288)
<matplotlib.figure.Figure at 0xb477790>
########################################
3.快速繪圖
arr1 = np.random.randn(100)
# print(arr1)
plt.plot(arr1)
plt.show()
運(yùn)行結(jié)果
4.Subplot
可以通過add_subplot來分割figure,表示可以在figure的不同位置上作圖
fig.add_subplot(a, b, c)
a,b 表示將fig分割成 a*b 的區(qū)域
c 表示當(dāng)前選中要操作的區(qū)域膝擂,
注意:從1開始編號(hào)(不是從0開始)
plot 繪圖的區(qū)域是最后一次指定subplot的位置 (jupyter notebook里不能正確顯示)
# jupyter 里不能顯示
arr2 = np.random.randn(100)
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
ax1.plot(arr2)
ax2.plot(arr2)
ax3.plot(arr2)
ax4.plot(arr2)
plt.show()
5. 直方圖hist方法
參數(shù)一:數(shù)據(jù)集
bins參數(shù):代表展現(xiàn)數(shù)據(jù)的直方個(gè)數(shù)
color參數(shù):可以指定顏色
Alpha:參數(shù)可以指定透明度(默認(rèn)是1虑啤,表示不透明)
plt.hist(arr2,bins=20,color='k',alpha=0.5)
plt.show()
運(yùn)行結(jié)果
6. 散點(diǎn)圖 scatter方法
參數(shù)1:x軸的坐標(biāo)
參數(shù)2: y軸的坐標(biāo)
x = [1,2,3,4,5]
y = [5,6,7,4,3]
plt.scatter(x,y,color='r')
plt.show()
運(yùn)行結(jié)果
x = np.arange(10)
y = np.random.randint(0,5,10)
plt.scatter(x,y)
plt.show()
運(yùn)行結(jié)果
7. 柱形圖bar
x = np.arange(5)
y1,y2 = np.random.randint(1,25,size=(2,5))
width=0.3
plt.bar(x,y1,width,color='r',alpha = 0.6)
plt.bar(x+width,y2,width,color='b',alpha=0.6)
plt.show()
運(yùn)行結(jié)果
x = np.arange(4)
y1,y2 = np.random.randint(1,25,size=(2,4))
width = 0.2
ax = plt.subplot(1,1,1)
ax.bar(x,y1,width,color='r',alpha=0.6)
ax.bar(x+width,y2,width,color='b',alpha=0.6)
# 指定x軸標(biāo)記的位置
ax.set_xticks(x+width/2)
ax.set_xticklabels(['q1','q2','q3','q4'])
plt.show()
運(yùn)行結(jié)果
8. 餅狀圖
sizes:每個(gè)標(biāo)簽占多大隙弛,會(huì)自動(dòng)去算百分比
explode:將某部分爆炸出來, 使用括號(hào)狞山,將第一塊分割出來全闷,數(shù)值的大小是分割出來的與其他兩塊的間隙
labels:定義餅狀圖的標(biāo)簽,標(biāo)簽是列表
Colors:每部分的顏色
labeldistance萍启,文本的位置離遠(yuǎn)點(diǎn)有多遠(yuǎn)总珠,1.1指1.1倍半徑的位置
autopct,圓里面的文本格式勘纯,%3.1f%%表示小數(shù)有三位局服,整數(shù)有一位的浮點(diǎn)數(shù)
shadow,餅是否有陰影
startangle:起始角度驳遵,0淫奔,表示從0開始逆時(shí)針轉(zhuǎn),為第一塊堤结。一般選擇從90度開始比較好看
pctdistance:百分比的text離圓心的距離
返回值:p_texts餅圖內(nèi)部文本的唆迁,l_texts餅圖外label的文本
# 調(diào)整圖形的大小寬高
plt.figure(figsize=(8,6))
# 定義餅狀圖上顯示的標(biāo)簽,列表
labels = ['IE','Chrome','Firefox']
# 每個(gè)塊的大小竞穷,百分比
sizes = [40,50,10]
# 顏色
colors = ['y','k','r']
# 將某一部分爆炸出來唐责,元組()
explode = (0.1,0,0)
patches,l_text,p_text = plt.pie(sizes,explode=explode,labels=labels,colors=colors,
labeldistance=1.1,autopct="%3.1f%%",shadow=True,
startangle=90,pctdistance=0.6)
plt.show()
運(yùn)行結(jié)果
9. 矩陣?yán)L圖imshow
混淆矩陣,三個(gè)維度的關(guān)系
表示數(shù)據(jù)分布范圍情況
分布越多瘾带,值越大(值偏向1)妒蔚,顏色偏白,如果值越小顏色偏綠色
data = np.random.randn(10,10)
print(data)
#################運(yùn)行結(jié)果################
[[ 0.79216876 1.42931622 -1.7647661 1.62640919 0.66178082 0.04921573
-0.76783192 1.07053169 1.40744264 -0.13851484]
[-1.53587478 0.88159123 0.77804888 1.17960306 -1.6264733 0.48013081
1.13327399 1.79135941 -1.0195475 1.14625459]
[ 2.78574643 -0.21493639 1.34915968 1.18572988 -0.18755706 -0.03882507
-0.21560904 0.90186994 0.19319313 1.28486583]
[ 0.2776058 0.10872493 -1.47960929 -1.19917445 0.54804898 0.34829874
-1.18583962 0.14511466 -1.31990892 -0.11012531]
[-0.37740773 1.95613448 0.56153778 0.43202784 1.5774585 0.74983994
0.65840562 0.79909888 1.44862456 -1.55017949]
[-0.37113854 -1.76736113 -2.01355381 -0.61376981 -1.58085291 1.01602926
-1.1105543 0.69310044 0.50535768 -0.35909564]
[ 0.24726569 -0.0084276 -1.149235 0.58459508 1.26921766 -0.03779366
1.99952939 0.32946322 0.36575931 1.13901872]
[-0.82789188 1.22245847 1.30253428 -2.03761498 0.14996945 0.54857007
-0.46994465 0.22950404 1.07208546 -0.0074044 ]
[ 0.83339981 -0.47487887 1.66319774 -0.57931878 2.30565429 -0.24795773
-0.04656456 -0.74630762 -0.30773271 -0.10209038]
[-0.1210547 0.28764046 0.19531414 0.76053103 0.67383264 0.51694679
0.03379526 -0.08532095 1.44249721 0.74751775]]
########################################
plt.imshow(data,cmap=plt.cm.ocean)
plt.colorbar()
plt.show()
運(yùn)行結(jié)果
10. plt.subplots()
同時(shí)返回新創(chuàng)建的figure和subplot對(duì)象列表
在jupyter里可以正常顯示月弛,推薦使用這種方式創(chuàng)建多個(gè)圖表
fig,subplot_arr = plt.subplots(2,2)
print(type(subplot_arr))
subplot_arr[0,1].hist(np.random.randn(100),bins = 20, color='b',alpha=0.4)
subplot_arr[1,1].imshow(np.random.rand(5,5))
plt.show()
運(yùn)行結(jié)果
11. 顏色、標(biāo)記科盛、線型
顏色
標(biāo)記
線型
fig,subplot_arr = plt.subplots(2)
arr5 = np.random.randint(0,100,20)
arr6 = np.random.randint(0,100,20)
subplot_arr[0].plot(arr5,'ro-')
subplot_arr[1].plot(arr6,color='b',linestyle='dotted',marker='o')
plt.show()
運(yùn)行結(jié)果
12. 刻度帽衙、標(biāo)簽、圖例
設(shè)置刻度范圍
plt.xlim([xmin,xmax]), plt.ylim([ymin,ymax])
ax.set_xlim(), ax.set_ylim()
設(shè)置顯示的刻度
plt.xticks(list), plt.yticks(list)
ax.set_xticks(list), ax.set_yticks(list)
設(shè)置刻度標(biāo)簽
ax.set_xticklabels()
ax.set_yticklabels()
設(shè)置坐標(biāo)軸標(biāo)簽
ax.set_xlabel(list), ax.set_ylabel(list)
設(shè)置標(biāo)題
ax.set_title()
fig,ax = plt.subplots(1)
ax.plot(np.random.randn(1000).cumsum(),label='line0')
ax.plot(np.random.randn(1000).cumsum(),label='line1')
ax.plot(np.random.randn(1000).cumsum(),label='line2')
# 設(shè)置刻度
ax.set_xlim([0,500])
# 設(shè)置x軸的顯示刻度
ax.set_xticks(range(0,800,100))
# y標(biāo)簽
ax.set_yticklabels(['Jan','Feb','Mar'])
# 坐標(biāo)軸標(biāo)簽
ax.set_xlabel('number')
ax.set_ylabel('month')
# 標(biāo)題
ax.set_title('Example')
# 圖例
ax.legend(loc='best')
plt.show()