Python subplots() 使用说明

plt.subplots():
官方教程: link.

参数:
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)[source]
参数说明:
nrows:行数(默认1)
ncols:列数(默认1)
sharex:是否共享x轴
sharey:是否共享y轴

sharex, sharey的类型:
sharex, sharey:bool or {‘none’, ‘all’, ‘row’, ‘col’}, default: False

x或者y共享的控制方法:
Controls sharing of properties among x (sharex) or y (sharey) axes:
True or ‘all’: x- or y-axis will be shared among all subplots.
False or ‘none’: each subplot x- or y-axis will be independent.
‘row’: each subplot row will share an x- or y-axis.
‘col’: each subplot column will share an x- or y-axis.

使用案例:

# First create some toy data:
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

# Create just a figure and only one subplot
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')

# Create two subplots and unpack the output array immediately
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)

# Create four polar axes and access them through the returned array
fig, axs = plt.subplots(2, 2, subplot_kw=dict(polar=True))
axs[0, 0].plot(x, y)
axs[1, 1].scatter(x, y)

# Share a X axis with each column of subplots
plt.subplots(2, 2, sharex='col')

# Share a Y axis with each row of subplots
plt.subplots(2, 2, sharey='row')

# Share both X and Y axes with all subplots
plt.subplots(2, 2, sharex='all', sharey='all')

# Note that this is the same as
plt.subplots(2, 2, sharex=True, sharey=True)

# Create figure number 10 with a single subplot
# and clears it if it already exists.
fig, ax = plt.subplots(num=10, clear=True)

你可能感兴趣的:(Python subplots() 使用说明)