我需要在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
与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条目和受限布局指南
你可以用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文件控制
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,但增加它似乎只会让所有的图变得更小,而不能解决重叠问题。”
因此,为了使更多的空白和保持子图大小,总图像需要更大。