【问题标题】:Set size of subplot in matplotlib在 matplotlib 中设置子图的大小
【发布时间】:2017-05-22 16:59:40
【问题描述】:

我想知道当图形包含多个子图(在我的情况下为 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')

【问题讨论】:

  • 尝试设置正方形大小,即figsize=(20,20)。您的纵横比设置几乎是正方形的子图,但数字不是。

标签: python matplotlib subplot


【解决方案1】:

只需从以下位置切换图形大小宽度和高度:

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

到:

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

将整个页面用于绘图。

这将改变你的情节:

到:

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 2019-04-30
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    相关资源
    最近更新 更多