繁体   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