Matplotlib子图过于狭窄,布局紧凑 [英] Matplotlib Subplots Are Too Narrow With Tight Layout

查看:68
本文介绍了Matplotlib子图过于狭窄,布局紧凑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用GridSpec在Matplotlib(Python 3.6,Matplotlib 2.0.0)中绘制许多子图.这是最小的工作示例:

I am currently trying to plot many subplots in Matplotlib (Python 3.6, Matplotlib 2.0.0) using GridSpec. Here is the minimal working example:

import matplotlib.pyplot as plt
from matplotlib.gridspec import *

# Color vector for scatter plot points
preds = np.random.randint(2, size=100000)

# Setup the scatter plots
fig = plt.figure(figsize=(8,8))
grid = GridSpec(9, 9)

# Create the scatter plots
for ii in np.arange(0, 9):
    for jj in np.arange(0, 9):
        if (ii > jj):
            ax = fig.add_subplot(grid[ii, jj])
            x = np.random.rand(100000)*2000
            y = np.random.rand(100000)*2000
            ax.scatter(x, y, c=preds)

这是未经任何修改的结果:

This is the result without any modifications:

当然子图之间的间距并不令人满意,所以我做了我通常做的事情并使用了tight_layout().但如下图所示,tight_layout() 无法接受地挤压了图的宽度:

Of course the spacing between subplots is unsatisfactory so I did what I usually do and used tight_layout(). But as can be seen in the figure below, tight_layout() squeezes the width of the plots unacceptably:

我认为我应该只使用 subplots_adjust()手动调整子图,而不是使用 tight_layout().下图为subplots_adjust(hspace=1.0, wspace=1.0).

Instead of using tight_layout(), I figured I should just adjust the subplots manually using subplots_adjust(). Below is the figure with subplots_adjust(hspace=1.0, wspace=1.0).

结果几乎是正确的,再稍微调整一下子图之间的空间就完美了.然而,子图似乎太小而无法充分传达信息.

The result is almost correct, and with a little more tweaking the space between subplots would be perfect. However the subplots appear too small to adequately convey information.

在保持纵横比和足够大的子图尺寸的同时,是否有更好的方法在子图之间保持适当的间距?我能想到的唯一可能的解决方案是使用 subplots_adjust() 和更大的 figsize,但这会导致图形边缘和子图.

Is there a better way to get proper spacing between subplots while still maintaining aspect ratio and a large enough subplot size? The only possible solution I could come up with was to use subplots_adjust() with a larger figsize, but this results in a very large space between the edges of the figure and the subplots.

任何解决方案都值得赞赏.

Any solutions are appreciated.

推荐答案

由于您所有的轴都具有相同的 x y 范围,因此我选择显示对勾标签仅在外部 Axes 上.对于相同大小的子图网格,使用 plt.subplots()sharexsharey 关键字可以轻松实现自动化.当然,如果您设置9x9子图的网格,则可以提供比您想要的更多的图,但是您可以使冗余图不可见(例如,使用 Axes.set_visible 或完全删除它们).在下面的示例中,我将使用后者.

As all your axes have the same x and y ranges, I would choose to show the tick labels only on the outer Axes. For a grid of equally-sized subplots, this is easily automated with the sharex and sharey keywords of plt.subplots(). Of course, if you set up a grid of 9x9 subplots, that gives you more plots than you want, but you can either make the redundant plots invisible (for instance with Axes.set_visible or remove them entirely. In the example below I go with the latter.

from matplotlib import pyplot as plt
import numpy as np

fig, axes = plt.subplots(
    nrows=9, ncols=9, sharex=True, sharey=True, figsize = (8,8)
)

# Color vector for scatter plot points
preds = np.random.randint(2, size=1000)

# Create the scatter plots
for ii in np.arange(0, 9):
    for jj in np.arange(0, 9):
        if (ii > jj):
            ax = axes[ii,jj]
            x = np.random.rand(1000)*100
            y = np.random.rand(1000)*2000
            ax.scatter(x, y, c=preds)
        else:
            axes[ii,jj].remove() ##remove Axes from fig
            axes[ii,jj] = None   ##make sure that there are no 'dangling' references.    

plt.show()

结果图形如下:

当然可以使用 subplots_adjust()之类的东西进一步调整.希望这会有所帮助.

This can be of course adjusted further with something like subplots_adjust(). Hope this helps.

这篇关于Matplotlib子图过于狭窄,布局紧凑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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