Python Matplotlib-对齐栅格

Python Matplotlib-对齐栅格,python,matplotlib,Python,Matplotlib,我正在使用Matplotlib.GridSpec绘制图像下方的直方图,如下面代码所示: import imageio import numpy as np import matplotlib.pyplot as plt from matplotlib import gridspec plt.close('all') plt.style.use('ggplot') img = imageio.imread('imageio:page.png') y = np.bincount(img.rav

我正在使用
Matplotlib.GridSpec
绘制图像下方的直方图,如下面代码所示:

import imageio
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

plt.close('all')
plt.style.use('ggplot')

img = imageio.imread('imageio:page.png')

y = np.bincount(img.ravel(), minlength=256)
y = y/np.sum(y)
x = np.arange(len(y))

fig = plt.figure(figsize=(6,8))
gs = gridspec.GridSpec(2, 1, height_ratios=[6,1], width_ratios=[1])

ax0 = plt.subplot(gs[0])
ax0.imshow(img, cmap='gray')
ax0.xaxis.set_visible(False)
ax0.yaxis.set_visible(False)

ax1 = plt.subplot(gs[1])
ax1.fill_between(x, y)
ax1.yaxis.set_visible(False)
ax1.set_xlim([0,255])

fig.tight_layout()

plt.show()
当我们选择正确的体形尺寸时,图像会很好地对齐,如中所示

但是,如果没有正确选择图形大小,直方图显示的图像大小太大或太远,我们可以在下面看到


有没有办法告诉matplotlib正确对齐,即将直方图放在图像下方固定数量的像素处,并且直方图的拉伸长度永远不要超过图像宽度。

似乎您没有指定列gs[1:]。因为我只有两行和一列
gs[1]
相当于
gs[1://code>,如果您不想扭曲图像,使用GridSpec是不可能的。相反,需要使用an或an。谢谢。永远不要去管他们。我查一下。