1. 程式人生 > >Matplotlib-多圖合並顯示

Matplotlib-多圖合並顯示

寬高 畫出 顏色 itl label plot 窗口 bsp har

Subplot 多合一顯示

均勻圖中圖
不均勻圖中圖
# 均勻圖中圖 
# matplotlib 是可以組合許多的小圖, 放在一張大圖裏面顯示的. 使用到的方法叫作 subplot.

# 使用import導入matplotlib.pyplot模塊, 並簡寫成plt. 使用plt.figure創建一個圖像窗口.

import matplotlib.pyplot as plt

plt.figure()

#使用plt.subplot來創建小圖. plt.subplot(2,2,1)表示將整個圖像窗口分為2行2列, 當前位置為1. 使用plt.plot([0,1],[0,1])在第1個位置創建一個小圖.
plt.subplot(2,2,1) plt.plot([0,1],[0,1])
技術分享圖片
#plt.subplot(2,2,2)表示將整個圖像窗口分為2行2列, 當前位置為2. 使用plt.plot([0,1],[0,2])在第2個位置創建一個小圖.
plt.subplot(2,2,2)
plt.plot([0,1],[0,2])
# plt.subplot(2,2,3)表示將整個圖像窗口分為2行2列,當前位置為3. plt.subplot(2,2,3)可以簡寫成plt.subplot(223), matplotlib同樣可以識別. 使用plt.plot([0,1],[0,3])在第3個位置創建一個小圖.
plt.subplot(223) plt.plot([0,1],[0,3]) # plt.subplot(224)表示將整個圖像窗口分為2行2列, 當前位置為4. 使用plt.plot([0,1],[0,4])在第4個位置創建一個小圖. plt.subplot(224) plt.plot([0,1],[0,4]) plt.show() # 展示
技術分享圖片
# 不均勻圖中圖


# 如果希望展示的小圖的大小不相同, 應該怎麽做呢? 以上面的4個小圖為例, 如果把第1個小圖放到第一行, 而剩下的3個小圖都放到第二行.

#使用plt.subplot(2,1,1)將整個圖像窗口分為2行1列, 當前位置為1. 使用plt.plot([0,1],[0,1])在第1個位置創建一個小圖.
plt.subplot(2,1,1) plt.plot([0,1],[0,1]) # 使用plt.subplot(2,3,4)將整個圖像窗口分為2行3列, 當前位置為4. 使用plt.plot([0,1],[0,2])在第4個位置創建一個小圖. plt.subplot(2,3,4) plt.plot([0,1],[0,2]) # 上一步中使用plt.subplot(2,1,1)將整個圖像窗口分為2行1列, 第1個小圖占用了第1個位置, 也就是整個第1行. 這一步中使用plt.subplot(2,3,4)將整個圖像窗口分為2行3列, 於是整個圖像窗口的第1行就變成了3列, 也就是成了3個位置, 於是第2行的第1個位置是整個圖像窗口的第4個位置. #使用plt.subplot(235)將整個圖像窗口分為2行3列,當前位置為5. 使用plt.plot([0,1],[0,3])在第5個位置創建一個小圖. 同上, 再創建plt.subplot(236). plt.subplot(235) plt.plot([0,1],[0,3]) plt.subplot(236) plt.plot([0,1],[0,4]) plt.show() # 展示
技術分享圖片

Subplot 分格顯示

subplot2grid
gridspec
subplots

matplotlib 的 subplot 還可以是分格的,這裏介紹三種

# subplot2grid 
# 使用import導入matplotlib.pyplot模塊, 並簡寫成plt. 使用plt.figure()創建一個圖像窗口
import matplotlib.pyplot as plt
plt.figure()
# 使用plt.subplot2grid來創建第1個小圖, (3,3)表示將整個圖像窗口分成3行3列, (0,0)表示從第0行第0列開始作圖,colspan=3表示列的跨度為3, rowspan=1表示行的跨度為1. colspan和rowspan缺省, 默認跨度為1.

ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax1.plot([1, 2], [1, 2])    # 畫小圖
ax1.set_title(ax1_title)  # 設置小圖的標題

# 使用plt.subplot2grid來創建第2個小圖, (3,3)表示將整個圖像窗口分成3行3列, (1,0)表示從第1行第0列開始作圖,colspan=2表示列的跨度為2. 同上畫出 ax3, (1,2)表示從第1行第2列開始作圖,rowspan=2表示行的跨度為2. 再畫一個 ax4 和 ax5, 使用默認 colspan, rowspan.
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))

# 使用ax4.scatter創建一個散點圖, 使用ax4.set_xlabel和ax4.set_ylabel來對x軸和y軸命名.

ax4.scatter([1, 2], [2, 2])
ax4.set_xlabel(ax4_x)
ax4.set_ylabel(ax4_y)
技術分享圖片
# gridspec

# 使用import導入matplotlib.pyplot模塊, 並簡寫成plt. 使用import導入matplotlib.gridspec, 並簡寫成gridspec.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# 使用plt.figure()創建一個圖像窗口, 使用gridspec.GridSpec將整個圖像窗口分成3行3列.

plt.figure()
gs = gridspec.GridSpec(3, 3)

#使用plt.subplot來作圖, gs[0, :]表示這個圖占第0行和所有列, gs[1, :2]表示這個圖占第1行和第2列前的所有列, gs[1:, 2]表示這個圖占第1行後的所有行和第2列, gs[-1, 0]表示這個圖占倒數第1行和第0列, gs[-1, -2]表示這個圖占倒數第1行和倒數第2列.

ax6 = plt.subplot(gs[0, :])
ax7 = plt.subplot(gs[1, :2])
ax8 = plt.subplot(gs[1:, 2])
ax9 = plt.subplot(gs[-1, 0])
ax10 = plt.subplot(gs[-1, -2])
技術分享圖片
# subplots 
# 使用plt.subplots建立一個2行2列的圖像窗口,sharex=True表示共享x軸坐標, sharey=True表示共享y軸坐標. ((ax11, ax12), (ax13, ax14))表示第1行從左至右依次放ax11和ax12, 第2行從左至右依次放ax13和ax14.

f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)
# 使用ax11.scatter創建一個散點圖.
ax11.scatter([1,2], [1,2])
# plt.tight_layout()表示緊湊顯示圖像, plt.show()表示顯示圖像.
plt.tight_layout()
plt.show()
技術分享圖片

圖中圖

# 圖中圖(plot in plot)

# 數據
# 首先是一些準備工作:
# 導入pyplot模塊
import matplotlib.pyplot as plt

# 初始化figure
fig = plt.figure()

# 創建數據
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]
# 大圖 
# 接著,我們來繪制大圖。首先確定大圖左下角的位置以及寬高:
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# 註意,4個值都是占整個figure坐標系的百分比。在這裏,假設figure的大小是10x10,那麽大圖就被包含在由(1, 1)開始,寬8,高8的坐標系內。

# 將大圖坐標系添加到figure中,顏色為r(red),取名為title:
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, r)
ax1.set_xlabel(x)
ax1.set_ylabel(y)
ax1.set_title(title)
技術分享圖片
# 小圖
# 接著,我們來繪制左上角的小圖,步驟和繪制大圖一樣,註意坐標系位置和大小的改變:
# 左上角
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(y, x, b)
ax2.set_xlabel(x)
ax2.set_ylabel(y)
ax2.set_title(title inside 1)

# 右下角
plt.axes([0.6, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, g) # 註意對y進行了逆序處理
plt.xlabel(x)
plt.ylabel(y)
plt.title(title inside 2)
技術分享圖片

次坐標軸

主次坐標軸

# 次坐標軸
# 第一個y坐標
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x**2
y2 = -1 * y1
# 可以看到,y2和y1是互相倒置的。接著,獲取figure默認的坐標系 ax1:
fig, ax1 = plt.subplots()

# 第二個y坐標
# 對ax1調用twinx()方法,生成如同鏡面效果後的ax2:

ax2 = ax1.twinx()
# 接著進行繪圖, 將 y1, y2 分別畫在 ax1, ax2 上:
ax1.plot(x, y1, g-)   # green, solid line
ax1.set_xlabel(X data)
ax1.set_ylabel(Y1 data, color=g)
ax2.plot(x, y2, b-) # blue
ax2.set_ylabel(Y2 data, color=b)
plt.show()
技術分享圖片

Matplotlib-多圖合並顯示