无法删除 imshow() 图周围的 matplotlib 填充

Can't remove matplotlib's padding around imshow() figure

我正在将 matplotlib 嵌入到我的 PyQt4 GUI 中,我玩得很开心。我可以让图像显示,但它在我想删除的内容周围添加了一个非常厚的填充。这是我正在做的事情:

from PyQt4.QtCore import *
from PyQt.QtGui import *

import numpy as np

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4Agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.image as mpImage
import matplotlib.pyplot as plt

class MatPlotLibImage(FigureCanvas):
    def __init__(self):
    super(MatPlotLibImage, self).__init__(self.fig)
    self.axes = self.fig.add_subplot(111)

    def LoadImage():
        image = mpImage.imread("myImage.png")
        imgplot = self.axes.imshow(image, interpolation="nearest")
        # plt.axis("Off") -> Doesn't do anything as far as I can tell
        imgplot.axes.set_axis_off() # Gets rid of frame
        imgplot.axes.get_xaxis().set_visible(False) # Turn off x-axis
        imgplot.axes.get_yaxis().set_visible(False) # Turn off y-axis

如果我将此小部件添加到 QDockWidget,我会得到以下结果:

如您所见,它在内容周围呈现了一个大的白色填充。我似乎无法删除它,我在网上找到的所有内容都集中在保存图像时删除填充,而不是显示。有谁知道如何在显示时删除此填充?提前致谢。

您可以使用 subplots_adjust 去除边距。即

self.fig.subplots_adjust(bottom=0, top=1, left=0, right=1)

这将告诉图窗不要在其子轴周围使用任何边距。然后,您可能仍然会在一个方向上看到一些白色 space,这是因为 canvas 纵横比与图像纵横比不同。但是,我认为您不想更改图像纵横比,因此实际上需要这个剩余边距。