简体   繁体   English

matplotlib // 子图之间的空间

[英]matplotlib // space between subplots

I want to plot images (in the 1st row) along with some diagrams (the 2nd and 3rd rows) using subplots from matplotlib.pyplot .我想使用matplotlib.pyplot subplots图绘制图像(在第一行)以及一些图表(第二行和第三行)。 However, imshow fucntion adds some additional white space around images I can't get rid of.但是, imshow fucntion 在我无法摆脱的图像周围添加了一些额外的空白。 Here is my code and the plot I'm getting:这是我的代码和我得到的情节:

rcParams['figure.figsize'] = (16, 14)
_, axes = plt.subplots(3, 3)

axes[0][0].imshow(image)
axes[0][0].set_title('title')
axes[0][0].set_xticklabels(list())
axes[0][0].set_yticklabels(list())
axes[0][0].grid(False)

axes[0][1].imshow(image)
axes[0][1].set_title('title')
axes[0][1].set_xticklabels(list())
axes[0][1].set_yticklabels(list())
axes[0][1].grid(False)

axes[0][2].imshow(image)
axes[0][2].set_title('title')
axes[0][2].set_xticklabels(list())
axes[0][2].set_yticklabels(list())
axes[0][2].grid(False)

plt.savefig(file_name, bbox_inches='tight')

in the plot below you can clearly see that there is significantly more space between the 1st and 2nd rows:在下图中,您可以清楚地看到第一行和第二行之间有明显更多的空间:

我得到的情节

I would like to have an equal space between all subplots.我想在所有子图之间有一个相等的空间。 What would be the easiest way to do this?什么是最简单的方法来做到这一点?

Thanks in advance for any advice!提前感谢您的任何建议!

Best, Alexey最好的,阿列克谢

This is because imshow is showing the image with square pixels.这是因为imshow以方形像素显示图像。 If the image as a ratio of eg 16:9, the subplot will be reshaped to fit the image size.如果图像的比例为 16:9,则子图将重新调整形状以适应图像大小。 They will therefore have a different shape from the other subplots (see the imshow documentation for more info).因此,它们将具有与其他子图不同的形状(有关更多信息,请参阅imshow 文档)。

From here, you have two solutions:从这里,您有两个解决方案:

  • decrease the figure height in order to reduce manually the vertical space between subplots减少图形高度以手动减少子图之间的垂直空间
  • prevent imshow to resize the axes based on the images.防止imshow根据图像调整轴的大小。 For this you can set the aspect ratio to automatic aspect="auto" , and the image will fit the existing axes为此,您可以将纵横比设置为自动aspect="auto" ,图像将适合现有轴

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

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