matplotlib之plt.subplot

简介

matplotlib.pyplot.subplot:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html

返回一个指定子图分布的网格位置的 figure 对象。

subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)

分别指定(行数、列数、位置)

其他参数

  • facecolor: 背景色
  • polar: 是否用极坐标投影
  • projection: 投影

基本使用

import matplotlib.pyplot as plt
import numpy as np

# plot a line, implicitly creating a subplot(111)
plt.plot([1, 2, 3])
plt.show()

# now create a subplot which represents the top plot of a grid
# with 2 rows and 1 column. Since this subplot will overlap the
# first, the plot (and its axes) previously created, will be removed
plt.subplot(211)
plt.plot(range(12))
plt.subplot(212, facecolor='y')  # creates 2nd subplot with yellow background

plt.show()

matplotlib_subplot_basic

matplotlib_subplot_basic

import matplotlib.pyplot as plt
import numpy as np

'''
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
在指定的间隔内返回均匀间隔的数字,返回num均匀分布的样本在[start, stop]之间
'''
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')

plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')

plt.show()

matplotlib_subplot_numpy

import matplotlib.pyplot as plt
import numpy as np

square = np.zeros((32, 32))

square[5:10, 5:10] = 1
plt.subplot(221)
plt.imshow(square)

square[15:20, 5:10] = 1
plt.subplot(222)
plt.imshow(square)

square[5:10, 15:20] = 1
plt.subplot(223)
plt.imshow(square)

square[15:20, 15:20] = 1
plt.subplot(224)
plt.imshow(square)

plt.show()

matplotlib_subplot_square

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/04/01/matplotlib-plt-subplot/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
matplotlib之plt.subplot
简介 matplotlib.pyplot.subplot:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html 返回一个指定子图分布的网格位置的 figure 对象。 su……
<<上一篇
下一篇>>
文章目录
关闭
目 录