Matplotlib-繪圖區(qū)域
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['Arial Unicode MS', 'Microsoft Yahei', 'SimHei', 'sans-serif']
繪圖,簡(jiǎn)寫(xiě)
plt.plot([3,5,1,8,2])
[<matplotlib.lines.Line2D at 0x8211d68>]

output_3_1.png
等價(jià)于正常寫(xiě)法,面向過(guò)程(常用)
plt.figure(1) # 創(chuàng)建figure父窗口,默認(rèn)編號(hào)為1
plt.subplot(111) # 創(chuàng)建ax子窗口,默認(rèn):1行 1列 選中第1個(gè)
plt.plot([3,5,1,8,2]) # 在子圖上畫(huà)折線(xiàn)
[<matplotlib.lines.Line2D at 0x83527b8>]

output_5_1.png
面向?qū)ο髮?xiě)法(了解),將窗口對(duì)象賦給變量
# fig = plt.figure(2) # 創(chuàng)建父對(duì)象
# ax = fig.add_subplot(111) # 創(chuàng)建子對(duì)象
# ax.plot([1,2,3,4]) # 給子對(duì)象繪圖
# 前兩句合并
fig, ax = plt.subplots() # 創(chuàng)建父圖和子圖
ax.plot([1,2,3,4]) # 子圖繪制圖像
[<matplotlib.lines.Line2D at 0x82832e8>]

output_7_1.png
創(chuàng)建多個(gè)figure父對(duì)象
plt.figure() # 不寫(xiě)父對(duì)象號(hào)碼,默認(rèn)是1
plt.plot([1,2,3])
plt.figure(2)
plt.plot([4,1,6])
# 繪制到第二個(gè)父圖中
plt.plot([1,9,2])
# 繪制到第1個(gè)父圖中
plt.figure(1)
plt.plot([2,9,1])
[<matplotlib.lines.Line2D at 0x55bc748>]

output_9_1.png

output_9_2.png
父圖參數(shù)
plt.figure(
2, # 圖像編號(hào),創(chuàng)建和選擇圖像使用
figsize=(18, 5), # 圖片大小
facecolor='#cccccc', # 背景顏色
# dpi=300, # 分辨率,電腦看72,打印300起
)
plt.plot([2,9,3])
[<matplotlib.lines.Line2D at 0x9952c88>]

output_11_1.png
創(chuàng)建多個(gè)ax子對(duì)象
plt.figure(figsize=(10, 12))
plt.subplot(3,2,1) # 3行2列,第一張
plt.plot([1,2,3])
plt.subplot(322) # 行列式1位數(shù)時(shí),逗號(hào)可以省略
plt.subplot(323)
plt.subplot(324)
plt.plot([1,9,2])
plt.subplot(325)
plt.subplot(326)
# 調(diào)整子圖間距
plt.subplots_adjust(
hspace=0, # 行間距
wspace=0, # 列間距
)

output_13_0.png
fig父對(duì)象和ax子對(duì)象結(jié)合
# 圖1
plt.figure(1)
# 2個(gè)子圖
plt.subplot(1,2,1)
plt.subplot(1,2,2)
plt.plot([1,8,2])
# 圖2
plt.figure(2, figsize=(18, 5))
# 3個(gè)子圖
plt.subplot(1,3,1)
plt.subplot(1,3,2)
plt.scatter([1,2,3,4,5],[3,5,1,8,4])
plt.subplot(1,3,3)
<matplotlib.axes._subplots.AxesSubplot at 0xc8ca550>

output_15_1.png

output_15_2.png
面向?qū)ο蟮膄ig、ax結(jié)合的簡(jiǎn)寫(xiě) (了解)
# 簡(jiǎn)寫(xiě),單個(gè)圖
fig, ax = plt.subplots()
ax.plot([1,5,3,8,2])
ax.set_title('hello world')
# 簡(jiǎn)寫(xiě),多個(gè)圖
fig2, ax2 = plt.subplots(3,2) # 創(chuàng)建一個(gè)3行2列的圖表
ax2
ax2[0,1].plot([1,2,3]) # 繪圖,0,1表示選中第0行第1列的ax子圖
# 帶詳細(xì)參數(shù)
fig3, ax3 = plt.subplots(
figsize=(12,3), # 父窗口大小
nrows=2, # 子圖行數(shù)
ncols=3, # 子圖列數(shù)
sharex=True, # 是否共享x軸
sharey=True, # 是否共享y軸
)
ax3[0,0].plot([100,900,300])
ax3[0,1].plot([1,9,3])
[<matplotlib.lines.Line2D at 0xfd1fba8>]

output_17_1.png

output_17_2.png

output_17_3.png
復(fù)雜繪圖區(qū)域:pyplot子繪圖區(qū)域(了解)
設(shè)定網(wǎng)格,選中網(wǎng)格,確定選中行列區(qū)域數(shù)量,編號(hào)從0開(kāi)始
常用于數(shù)據(jù)面板,儀表板
plt.figure(1, figsize=(17,10))
plt.subplot2grid(
(4, 3),# 4行3列
(0, 0),# 選中0行0列單元格
colspan=3, #合并3列
)
plt.plot([2,1,9,5,4,8])
plt.subplot2grid((4,3),(1,0),rowspan=2,colspan=2) # 選中1行0列單元格,合并2行,合并2列
x = [1,3,5,7,9,11,13,15,17]
y = [2,-5,19,3,5,8,12,6,1]
plt.scatter(x, y)
plt.subplot2grid((4,3),(1,2),rowspan=2) #選中1行2列單元格,合并2行
plt.pie([2,6,9,15,1])
plt.subplot2grid((4,3),(3,0)) #選中3行0列單元格
plt.bar([1,2,3],[1,2,3])
plt.subplot2grid((4,3),(3,1)) #選中3行1列單元格
plt.bar([1,2,3],[3,9,1])
plt.subplot2grid((4,3),(3,2)) #選中3行2列單元格
plt.plot([3,5,2])
# 調(diào)整子圖間距
plt.subplots_adjust(
hspace=0, # 行間距
wspace=0, # 列間距
)

output_19_0.png