1. 程式人生 > >matplotlib的學習13-subplot分格顯示

matplotlib的學習13-subplot分格顯示

layout nbsp light pyplot 設置 out imp 標題 true

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))#分為3行3列,從第二行第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‘)


import matplotlib.gridspec as gridspec
plt.figure()
gs = gridspec.GridSpec(3, 3)#使用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])
plt.show()


# 使用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()

ax3 = plt.subplot2grid((3,3),(2,0))表示把整個figure分為了三行三列,從第二行第0列開始作圖

可以就是用網格作圖,方便

matplotlib的學習13-subplot分格顯示