Python 如何在共享轴的子地块上消除额外的空白?

Python 如何在共享轴的子地块上消除额外的空白?,python,matplotlib,Python,Matplotlib,我正在使用python 3.5.1和matplotlib 1.5.1创建一个绘图,它有两个子绘图(并排)和一个共享的Y轴。示例输出图像如下所示: 请注意,每组轴的顶部和底部都有额外的空白。不管我怎么努力,我似乎无法摆脱它。该图的总体目标是在左侧有一个瀑布型图,图的右侧有一个共享的Y轴 下面是一些复制上面图像的示例代码 import matplotlib.pyplot as plt import numpy as np import pandas as pd %matplotlib inline

我正在使用python 3.5.1和matplotlib 1.5.1创建一个绘图,它有两个子绘图(并排)和一个共享的Y轴。示例输出图像如下所示:

请注意,每组轴的顶部和底部都有额外的空白。不管我怎么努力,我似乎无法摆脱它。该图的总体目标是在左侧有一个瀑布型图,图的右侧有一个共享的Y轴

下面是一些复制上面图像的示例代码

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline

# create some X values

periods = np.linspace(1/1440, 1, 1000)

# create some Y values (will be datetimes, not necessarily evenly spaced 
# like they are in this example)

day_ints = np.linspace(1, 100, 100)
days = pd.to_timedelta(day_ints, 'D') + pd.to_datetime('2016-01-01')

# create some fake data for the number of points 
points = np.random.random(len(day_ints))

# create some fake data for the color mesh

Sxx = np.random.random((len(days), len(periods)))

# Create the plots

fig = plt.figure(figsize=(8, 6))

# create first plot

ax1 = plt.subplot2grid((1,5), (0,0), colspan=4)
im = ax1.pcolormesh(periods, days, Sxx, cmap='viridis', vmin=0, vmax=1)
ax1.invert_yaxis()
ax1.autoscale(enable=True, axis='Y', tight=True)

# create second plot and use the same y axis as the first one

ax2 = plt.subplot2grid((1,5), (0,4), sharey=ax1)    
ax2.scatter(points, days)
ax2.autoscale(enable=True, axis='Y', tight=True)

# Hide the Y axis scale on the second plot
plt.setp(ax2.get_yticklabels(), visible=False)    
#ax1.set_adjustable('box-forced') 
#ax2.set_adjustable('box-forced')

fig.colorbar(im, ax=ax1)
正如您在注释掉的代码中所看到的,我已经尝试了许多方法,如和之类的帖子所建议的


一旦我删除了第二个子Plot2Grid调用的
sharey=ax1
部分,问题就消失了,但是我也没有一个公共的Y轴。

自动缩放倾向于向数据添加一个缓冲区,以便所有数据点都很容易看到,而不是被轴部分切断

更改:

ax1.autoscale(enable=True, axis='Y', tight=True)
致:

致:

要获得:


通常我喜欢自动缩放“Help me”(帮助我),但当与图像(或看起来像图像的东西)结合使用时,额外的空白会让人讨厌。谢谢,我已经为此奋斗了一整天。
ax1.set_ylim(days.min(),days.max())
ax2.autoscale(enable=True, axis='Y', tight=True)
ax2.set_ylim(days.min(),days.max())