我需要在matplotlib中生成一大堆垂直堆叠的图。结果将使用savefig保存,并在网页上查看,所以我不关心最终图像有多高,只要子图之间有间隔,这样它们就不会重叠。

不管我让这个数字有多大,次要情节似乎总是重叠的。

我的代码目前看起来像

import matplotlib.pyplot as plt
import my_other_module

titles, x_lists, y_lists = my_other_module.get_data()

fig = plt.figure(figsize=(10,60))
for i, y_list in enumerate(y_lists):
    plt.subplot(len(titles), 1, i)
    plt.xlabel("Some X label")
    plt.ylabel("Some Y label")
    plt.title(titles[i])
    plt.plot(x_lists[i],y_list)
fig.savefig('out.png', dpi=100)

你可以用plt。Subplots_adjust用于更改子图之间的间距。

签名:

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

参数含义(以及建议的默认值)如下:

left  = 0.125  # the left side of the subplots of the figure
right = 0.9    # the right side of the subplots of the figure
bottom = 0.1   # the bottom of the subplots of the figure
top = 0.9      # the top of the subplots of the figure
wspace = 0.2   # the amount of width reserved for blank space between subplots
hspace = 0.2   # the amount of height reserved for white space between subplots

实际的默认值由rc文件控制


使用subplots_adjust(hspace=0)或一个非常小的数字(hspace=0.001)将完全删除子图之间的空白,而hspace=None则不会。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tic

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

x = np.arange(100)
y = 3.*np.sin(x*2.*np.pi/100.)

for i in range(1, 6):
    temp = 510 + i
    ax = plt.subplot(temp)
    plt.plot(x, y)
    plt.subplots_adjust(hspace=0)
    temp = tic.MaxNLocator(3)
    ax.yaxis.set_major_locator(temp)
    ax.set_xticklabels(())
    ax.title.set_visible(False)

plt.show()

Hspace =0或Hspace =0.001

hspace = band


请查看matplotlib:紧凑布局指南,并尝试使用matplotlib.pyplot。或者matplotlib.figure.Figure.tight_layout

举个简单的例子:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(8, 8))
fig.tight_layout() # Or equivalently,  "plt.tight_layout()"

plt.show()

布局不紧凑


紧凑的布局


import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10,60))
plt.subplots_adjust( ... )

plt。subplots_adjust方法:

def subplots_adjust(*args, **kwargs):
    """
    call signature::

      subplots_adjust(left=None, bottom=None, right=None, top=None,
                      wspace=None, hspace=None)

    Tune the subplot layout via the
    :class:`matplotlib.figure.SubplotParams` mechanism.  The parameter
    meanings (and suggested defaults) are::

      left  = 0.125  # the left side of the subplots of the figure
      right = 0.9    # the right side of the subplots of the figure
      bottom = 0.1   # the bottom of the subplots of the figure
      top = 0.9      # the top of the subplots of the figure
      wspace = 0.2   # the amount of width reserved for blank space between subplots
      hspace = 0.2   # the amount of height reserved for white space between subplots

    The actual defaults are controlled by the rc file
    """
    fig = gcf()
    fig.subplots_adjust(*args, **kwargs)
    draw_if_interactive()

or

fig = plt.figure(figsize=(10,60))
fig.subplots_adjust( ... )

图片的大小很重要。

“我试过改变hspace,但增加它似乎只会让所有的图变得更小,而不能解决重叠问题。”

因此,为了使更多的空白和保持子图大小,总图像需要更大。


您可以尝试.subplot_tool()

plt.subplot_tool()

与tight_layout类似,matplotlib现在(从2.2版开始)提供constrained_layout。与tight_layout(在单个优化布局的代码中可以随时调用)相反,constrained_layout是一个属性,它可以是活动的,并且会在每个绘制步骤之前优化布局。

因此,它需要在子图创建之前或创建期间被激活,例如figure(constrained_layout=True)或subplots(constrained_layout=True)。

例子:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(4,4, constrained_layout=True)

plt.show()

constrained_layout也可以通过rcParams来设置

plt.rcParams['figure.constrained_layout.use'] = True

请参阅what's new条目和受限布局指南


在使用pandas.DataFrame绘制数据帧时解决了此问题。Plot,它使用matplotlib作为默认后端。 下面的代码适用于指定的任何类型=(例如:'bar', 'scatter', 'hist'等等)。 在python 3.8.12, pandas 1.3.4, matplotlib 3.4.3中测试

导入和样例数据

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# sinusoidal sample data
sample_length = range(1, 15+1)
rads = np.arange(0, 2*np.pi, 0.01)
data = np.array([np.sin(t*rads) for t in sample_length])
df = pd.DataFrame(data.T, index=pd.Series(rads.tolist(), name='radians'), columns=[f'freq: {i}x' for i in sample_length])

# default plot with subplots; each column is a subplot
axes = df.plot(subplots=True)

调整间距

Adjust the default parameters in pandas.DataFrame.plot Change figsize: a width of 5 and a height of 4 for each subplot is a good place to start. Change layout: (rows, columns) for the layout of subplots. sharey=True and sharex=True so space isn't taken for redundant labels on each subplot. The .plot method returns a numpy array of matplotlib.axes.Axes, which should be flattened to easily work with. Use .get_figure() to extract the DataFrame.plot figure object from one of the Axes. Use fig.tight_layout() if desired.

axes = df.plot(subplots=True, layout=(3, 5), figsize=(25, 16), sharex=True, sharey=True)

# flatten the axes array to easily access any subplot
axes = axes.flat

# extract the figure object
fig = axes[0].get_figure()

# use tight_layout
fig.tight_layout()

df

# display(df.head(3))
         freq: 1x  freq: 2x  freq: 3x  freq: 4x  freq: 5x  freq: 6x  freq: 7x  freq: 8x  freq: 9x  freq: 10x  freq: 11x  freq: 12x  freq: 13x  freq: 14x  freq: 15x
radians                                                                                                                                                            
0.00     0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000
0.01     0.010000  0.019999  0.029996  0.039989  0.049979  0.059964  0.069943  0.079915  0.089879   0.099833   0.109778   0.119712   0.129634   0.139543   0.149438
0.02     0.019999  0.039989  0.059964  0.079915  0.099833  0.119712  0.139543  0.159318  0.179030   0.198669   0.218230   0.237703   0.257081   0.276356   0.295520

这个答案显示了在创建图形后使用fig.tight_layout。但是,tight_layout可以在创建图形时直接设置,因为matplotlib.pyplot.subplots接受带有**fig_kw的附加参数。所有附加的关键字参数都传递给pyplot。图的电话。 有关访问子图和在子图中绘图,请参阅如何在多个子图中绘图。

import matplotlib.pyplot as plt

# create the figure with tight_layout=True
fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(8, 8), tight_layout=True)