简体   繁体   English

我无法改变matplotlib中子图之间的距离

[英]I am not able to change the distance between to subplots in matplotlib

I am pretty new to python, and I am plotting two imshow objects into a figure, one below the other. 我是python的新手,我正在将两个imshow对象绘制成一个图形,一个在另一个之下。 However, I am not able to change the horizontal distance between the two objects, ie they are too far apart. 但是,我无法改变两个物体之间的水平距离,即它们相距太远。

As suggested in Improve subplot size/spacing with many subplots in matplotlib , I have tried using plt.tight_layout() and plt.subplots_adjust , but it didn't affect the plot at all. 正如使用matplotlib中的许多子图改进子图大小/间距所建议的那样,我尝试使用plt.tight_layout()plt.subplots_adjust ,但它根本不影响绘图。

This is how I created the plot (I left out the parts where I define the arrays to be plotted and the properties of the imshow plot): 这就是我创建绘图的方式(我省略了我定义要绘制的数组的部分以及imshow绘图的属性):

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.imshow(help_vector, cmap=cmap, norm=norm)
ax2 = fig.add_subplot(212)
ax2.imshow(plot_vector, cmap=cmap, norm=norm)

I dont't know if this is important, but both subplots have a title, the whole figure has a title, and the second subplot has a 'legend' next to it which I created by using the follwing code: 我不知道这是否重要,但两个子图都有一个标题,整个图有一个标题,第二个子图有一个“图例”,它是我用下面的代码创建的:

patches = [mpatches.Patch(color='lime', label = 'No relevant error'), mpatches.Patch(color='yellow',label='Medium error'), mpatches.Patch(color='orange',label='Large error'), mpatches.Patch(color='red',
    label='Unacceptable')]
    plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)

When storing the image with savefig , I added the argument bbox_inches='tight' . 使用savefig存储图像时,我添加了参数bbox_inches='tight'

Image of the plot 情节的图像

Any kind of help is very much appreciated! 非常感谢任何形式的帮助!

Edit: Added an image of the plot. 编辑:添加了图表的图像。

As @ImportanceOfBeingErnest pointed out, the solution was to use the top and bottom parameters of subplots_adjust() . 正如@ImportanceOfBeingErnest指出的那样,解决方案是使用subplots_adjust subplots_adjust()topbottom参数。 In my case, fig.subplots_adjust(top=0.7,bottom=0.3) was what I needed. 就我而言, fig.subplots_adjust(top=0.7,bottom=0.3)是我所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM