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

上一篇 numpy.linalg学习
下一篇 matplotlib之plt.subplots
目录
文章列表
1 The selected directory is not a valid home for Go SDK
The selected directory is not a valid home for Go SDK
2
Vue.js HTTP请求代理配置
Vue.js HTTP请求代理配置
3
MySQL 排序
MySQL 排序
4
DDL与DML的区别
DDL与DML的区别
5
Android接入Google Analytics记录
Android接入Google Analytics记录
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。