matplotlib pyplot:子图大小

gol*_*p04 2 python matplotlib

如果我绘制如下图所示的单个图形,则其大小(x*y).

import matplotlib.pyplot as plt
plt.plot([1, 2], [1, 2])
Run Code Online (Sandbox Code Playgroud)

但是,如果我在同一行中绘制3个子图,则每个子图的大小((x/3)*y).

fig, ax = plt.subplots(1, 3, sharey = True)
for i in range(3):
    ax[i].plot([1, 2], [1, 2])
Run Code Online (Sandbox Code Playgroud)

如何获得这3个子图,每个子图的大小(x*y)?

Gab*_*iel 5

图形对象具有默认大小,该大小不知道子图的数量.您可以在制作图形时更改图形尺寸以满足您的需要.

import matplotlib.pyplot as plt

nx = 3
ny = 1

dxs = 8.0
dys = 6.0

fig, ax = plt.subplots(ny, nx, sharey = True, figsize=(dxs*nx, dys*ny) )
for i in range(nx):
    ax[i].plot([1, 2], [1, 2])
Run Code Online (Sandbox Code Playgroud)