簡體   English   中英

matplotlib 使用子圖(如網格、行或列)繪制多個圖

[英]matplotlib plot multiple plots using subplots like grid, in row or in column

以下是一些必需的示例。

網格圖

網格圖

行圖

行明智圖

柱狀圖

列明智圖

垂直子圖:

fig, axs = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
axs[0].plot(x, y)
axs[1].plot(x, -y)

水平子圖:

python3
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)

網格子圖:

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()

subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)語法: subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)第一個參數 => nrows => 行數你想要,第二個參數 => ncols => 你想要的列數,

文檔文檔

import cv2
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
plt.rcParams['axes.grid'] = False
    
image_filepath="Resources/Lenna.png"
img = cv2.imread(image_filepath)
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)

# blur images with different kernel size
img_blur3 = cv2.GaussianBlur(img_gray,(3,3),0) #src, ksize, sigma
img_blur7 = cv2.GaussianBlur(img_gray,(7,7),0)
img_blur15 = cv2.GaussianBlur(img_gray,(15,15),0)

網格圖

這里 axs[x,y] 中的 x,y 表示網格中的坐標。

fig,axs = plt.subplots(2,2,figsize=(10,10))
axs[0,0].imshow(img_gray,cmap='gray')
axs[0,0].set_title("Original Image")
axs[1,0].imshow(img_blur3,cmap='gray')
axs[1,0].set_title("3x3")
axs[0,1].imshow(img_blur7,cmap='gray')
axs[0,1].set_title("7x7")
axs[1,1].imshow(img_blur15,cmap='gray')
axs[1,1].set_title("15x15")
plt.show()

網格圖

行圖

這里 axs[x] 中的 x 表示行號或列號。

fig,axs = plt.subplots(3,1,figsize=(5,15))
axs[0].imshow(img_blur3,cmap='gray')
axs[0].set_title("3x3")
axs[1].imshow(img_blur7,cmap='gray')
axs[1].set_title("7x7")
axs[2].imshow(img_blur15,cmap='gray')
axs[2].set_title("15x15")
plt.show()

行圖

柱狀圖

fig,axs = plt.subplots(1,3,figsize=(15,5))
axs[0].imshow(img_blur3,cmap='gray')
axs[0].set_title("3x3")
axs[1].imshow(img_blur7,cmap='gray')
axs[1].set_title("7x7")
axs[2].imshow(img_blur15,cmap='gray')
axs[2].set_title("15x15")
plt.show()

柱狀圖

使用這個小包,MatplotlibDashboard 為您提供了一個易於使用的界面。 請參閱鏈接中的文檔和示例。

pip install matplotlib-dashboard

網格圖:

from matplotlib_dashboard import MatplotlibDashboard
dashboard = MatplotlibDashboard([
    ['A','B','C'],
    ['D','E','F'],
    ['G','H','I'],
])

dashboard['A'].plot(list(range(100)), color='red')
dashboard['A'].set_title('A plot')

dashboard['I'].plot(list(range(100)), color='red')
dashboard['I'].set_title('I plot')

# more plots ...

plt.show()

行圖:

from matplotlib_dashboard import MatplotlibDashboard
dashboard = MatplotlibDashboard([
    ['A','B','C'],
])

dashboard['A'].plot(list(range(100)), color='red')
dashboard['A'].set_title('A plot')
# more plots ...
plt.show()

柱狀圖:

from matplotlib_dashboard import MatplotlibDashboard
dashboard = MatplotlibDashboard([
    ['A'],
    ['D'],
    ['G'],
])

dashboard['A'].plot(list(range(100)), color='red')
dashboard['A'].set_title('A plot')
# more plots ...
plt.show()

復雜的情節:

from matplotlib_dashboard import MatplotlibDashboard
dashboard = MatplotlibDashboard([
    ['top' ,'top' ,'top' ,'top'  ],
    ['left','left', None ,'right'],
    ['left','left','down','right'],
], as3D=['left'], wspace=0.5, hspace=0.5)

# drawing plots ...

plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM