简体   繁体   English

matplotlib 使用子图(如网格、行或列)绘制多个图

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

Here are some of the example which is required.以下是一些必需的示例。

Grid Plot网格图

网格图

Row Plot行图

行明智图

Column Plot柱状图

列明智图

Vertical Subplots :垂直子图:

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

Horizontal Subplots :水平子图:

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

Grid Subplots :网格子图:

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()

Syntax of subplot : subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw) First param => nrows => number of rows you want, Second param => ncols => number of columns you want, 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 => 你想要的列数,

Docs Docs 文档文档

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)

Grid Plot网格图

Here x,y in axs[x,y] represent coordinates in grid.这里 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()

网格图

Row Plot行图

Here x in axs[x] represents the row or column number.这里 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()

行图

Column Plot柱状图

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()

柱状图

Using this small package, MatplotlibDashboard provides an easy-to-use interface for your purpose.使用这个小包,MatplotlibDashboard 为您提供了一个易于使用的界面。 See the docs and the example in the link.请参阅链接中的文档和示例。

pip install matplotlib-dashboard

Grid Plot:网格图:

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()

Row Plot:行图:

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()

Column Plot:柱状图:

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()

Complex Plot:复杂的情节:

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