在matplotlib中设置子图的大小 [英] Set size of subplot in matplotlib

查看:132
本文介绍了在matplotlib中设置子图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当图形包含多个子图(本例中为5×2)时,我想知道如何设置子图的大小.无论我允许整个图形有多大,次要情节似乎总是很小.我想直接控制此图中子图的大小.代码的简化版粘贴在下面.

I wonder how to set the size of the subplot when figure contains multiple subplots (5 × 2 in my case). No matter how big I allow the whole figure to be, the subplots always seem to be small. I would like to have direct control of the size of the subplot in this figure. The simplified version of the code is pasted below.

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')

推荐答案

只需切换图形大小的宽度和高度即可:

Just switch figure size width and height from:

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

到:

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

使用整个页面进行绘图.

to use the whole page for your plots.

这将改变你的情节:

收件人:

这篇关于在matplotlib中设置子图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆