在matplotlib中设置子图的大小

And*_*rej 11 python matplotlib subplot

我想知道当图包含多个子图(在我的情况下为5×2)时如何设置子图的大小.无论我允许整个人物有多大,子图总是看起来很小.我希望能直接控制该图中子图的大小.代码的简化版本粘贴在下面.

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(20)
y = np.random.randn(20)

fig = plt.figure(figsize=(20, 8))

for i in range(0,10):
    ax = fig.add_subplot(5, 2, i+1)
    plt.plot(x, y, 'o')
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    # x and y axis should be equal length
    x0,x1 = ax.get_xlim()
    y0,y1 = ax.get_ylim()
    ax.set_aspect(abs(x1-x0)/abs(y1-y0))

plt.show()
fig.savefig('plot.pdf', bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)

Mik*_*ler 14

只需切换图形尺寸宽度和高度:

fig = plt.figure(figsize=(20, 8))
Run Code Online (Sandbox Code Playgroud)

至:

fig = plt.figure(figsize=(8, 20))
Run Code Online (Sandbox Code Playgroud)

使用整个页面为你的情节.

这将改变你的情节:

在此输入图像描述

至:

在此输入图像描述