subplot

pyplot 绘制多图方法


subplot

subplot()语法:
1
subplot(nrows, ncols, index, **kwargs)
  • nrows:行数
  • ncols:列数
  • index:从左到右,从上到下,每个子区域的编号$1\cdots N$
1
2
3
4
5
6
7
8
9
10
11
12
row, col = 3, 2

img = img.reshape(features, samples)

for i in range(1, row * col+1):
plt.subplot(row, col, i)
timg = img[:, i-1]
timg = timg.reshape(width, height)
plt.imshow(timg)
plt.xticks([])
plt.yticks([])
plt.show()

subplots

subplots()语法:
1
matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
  • nrows:默认为 1,设置图表的行数。
  • ncols:默认为 1,设置图表的列数。
  • sharex、sharey:设置 x、y 轴是否共享属性,默认为 false,可设置为 ‘none’、’all’、’row’ 或 ‘col’。 False 或 none 每个子图的 x 轴或 y 轴都是独立的,True 或 ‘all’:所有子图共享 x 轴或 y 轴,’row’ 设置每个子图行共享一个 x 轴或 y 轴,’col’:设置每个子图列共享一个 x 轴或 y 轴。
  • squeeze:布尔值,默认为 True,表示额外的维度从返回的 Axes(轴)对象中挤出,对于 N1 或 1N 个子图,返回一个 1 维数组,对于 N*M,N>1 和 M>1 返回一个 2 维数组。如果设置为 False,则不进行挤压操作,返回一个元素为 Axes 实例的2维数组,即使它最终是1x1。
  • subplot_kw:可选,字典类型。把字典的关键字传递给 add_subplot() 来创建每个子图。
  • gridspec_kw:可选,字典类型。把字典的关键字传递给 GridSpec 构造函数创建子图放在网格里(grid)
  • **fig_kw:把详细的关键字参数传给 figure() 函数。
1
2
3
4
5
6
7
8
9
10
# 创建画像和子图
fig, axs = plt.subplots(nrows, ncols)

###### 未对代码进行测试!
axs[0, 0].imshow(timg1)
axs[0, 0].axis('off')
axs[1, 1].imshow(timg2)
axs[1, 1].axis('off')
######
fig.show()

subplot
https://pandintelli.github.io/2021/12/29/subplot/
作者
Pand
发布于
2021年12月29日
许可协议