matplotlib之plt.subplot

简介

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

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

1
2
3
subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)

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

其他参数

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

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :