与this question非常相似,但不同的是,我的数字可以大到需要的程度。
我需要在matplotlib中生成一大堆垂直叠加的图。结果将使用figsave保存并在网页上查看,因此我不在乎最终图像有多高,只要子块是间隔的,这样它们就不会重叠。
不管我允许这个数字有多大,子块似乎总是重叠的。
我的代码当前看起来像
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
更改子块(source)之间的间距呼叫签名:
参数含义(和建议的默认值)为:
实际的默认值由rc文件控制
我发现subblots_adjust(hspace=0.001)最终对我有效。当我使用space=None时,每个图之间仍有空白。然而,将其设置为非常接近零的值似乎会迫使它们排队。我在这里上传的不是最优雅的代码,但是你可以看到hspace是如何工作的。
相关问题 更多 >
编程相关推荐