简体   繁体   English

如何控制matplotlib中图形周围的空白?(plt.tight_layout不起作用)

[英]How does one control whitespace around a figure in matplotlib?(plt.tight_layout does not work)

I have a matplotlib figure with 3 sub-plots. 我有一个带有3个子图的matplotlib图。 The consensus from stackexchange seems to be to use the plt.tight_layout in order to get rid of the whitespace around the figure. stackexchange的共识似乎是使用plt.tight_layout来消除图形周围的空白。 This does not solve the problem in my case. 就我而言,这不能解决问题。

My code is as follows: 我的代码如下:

import numpy as np
import os
import pandas as pd
import matplotlib as mpl
from matplotlib import pyplot as plt

my_dpi = 1500
plt.ion()

index = np.linspace(0,5,10)
combined = np.linspace(0,1,10)
case1 = np.linspace(0,1,10)
case2 = np.linspace(0,1,10)
case3 = np.linspace(0,1,10)

tsfont = {'fontname':'Times-Roman'}
mpl.rcParams.update({'font.size': 18})
mpl.rc('font',family='Arial')
ms = 0

f = plt.figure()
f.set_size_inches(7.5,10)
f.axison=False


lw = 2
asp = 5


axarr1 = plt.subplot(3,1,1, adjustable='box',aspect = asp)

axarr1.set_xlim(0,5,10)
axarr1.set_ylim(0,1)
axarr1.set_ylabel('y')
axarr1.set_xlabel('$\\tau$', fontsize =25)
p = axarr1.plot(index,combined,color='navy', linewidth=lw, label = "Healthy")
axarr1.xaxis.set_label_coords(0.5, -0.05)


'''
Duct 2
'''

axarr2 = plt.subplot(3,1,2, adjustable='box',aspect = asp)
#axarr2.set_aspect('auto')
axarr2.set_xlim(0,5,10)
axarr2.set_ylim(0,1)
axarr2.set_ylabel('y')
axarr2.set_xlabel('$\\tau$', fontsize = 25)
g = axarr2.plot(index,combined,color='navy', linewidth=lw)
axarr2.xaxis.set_label_coords(0.5, -0.05)

'''
Duct 3
'''

axarr3 = plt.subplot(3,1,3, adjustable='box',aspect = asp)

axarr3.set_xlim(0,5,10)
axarr3.set_ylim(0,1)
axarr3.set_ylabel('y')
axarr3.set_xlabel('$\\tau$', fontsize = 25)
w = axarr3.plot(index,combined,color='navy', linewidth=lw)
axarr3.xaxis.set_label_coords(0.5, -0.05)

#plt.tight_layout()
plt.show()

Without using plt.tight_layout() I get the following result 不使用plt.tight_layout()我得到以下结果

在此处输入图片说明

Uncommenting the relevant line gives me 取消注释相关行给我

显而易见的是,尽管垂直间距发生变化,但水平间距却没有变化

As is obvious, while the vertical spacing changes, the horizontal spacing does not 显而易见的是,尽管垂直间距发生变化,但水平间距却没有变化

I'd like to know how to get rid of the horizontal whitespacing to the left and right. 我想知道如何摆脱左右水平的空白。

The tight_layout option places the images closer to the borders. tight_layout选项将图像放置在靠近边界的位置。 However, in your case, there is nothing to fill this empty space with, so it doesn't work. 但是,在您的情况下,没有什么可以填充此空白空间,因此它不起作用。

If you want a narrower figure, you should change the horizontal dimension, eg: 如果要缩小图形,则应更改水平尺寸,例如:

plt.figure(..., tight_layout=True, figsize=(12, 5))

This is in inches, experiment with the numbers until it looks good. 以英寸为单位,尝试数字,直到看起来不错为止。

You could also use 2 by 2 subplots to keep a square figure and use the space better: 您还可以使用2 x 2子图来保持正方形并更好地利用空间:

plt.subplot(2, 2, ...)

although this may look suboptimal with a prime number of figures. 尽管在以质数表示的情况下看起来可能不是最理想的。

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

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