others - 提高subplot大小/matplotlib的间距

我需要在matplotlib中生成一堆垂直堆叠的图。

无论我允许数字有多大,子图总是重叠。


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.tight_layout

作为快速示例:


import matplotlib.pyplot as plt

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

plt.show()


enter image description here


布局紧凑enter image description here


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.5 # the amount of height reserved for white space between subplots

我发现subplots_adjust(hspace = 0.001)可以工作。


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

fig = plt.figure()

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

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

plt.show()

enter image description here


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

或者


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

图片的大小很重要。

...