【问题标题】:Can't remove matplotlib's padding around imshow() figure无法删除 matplotlib 在 imshow() 图周围的填充
【发布时间】:2017-11-23 17:07:21
【问题描述】:

我正在将 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,我会得到以下结果:

如您所见,它在内容周围使用大的白色填充进行渲染。我似乎无法删除它,我在网上出现的所有内容都专注于在保存图像时删除填充,而不是显示。有谁知道如何在显示时删除此填充?提前致谢。

【问题讨论】:

    标签: python-2.7 matplotlib pyqt4


    【解决方案1】:

    您可以使用subplots_adjust 来消除边距。即

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

    这将告诉图形不要在其子轴周围使用任何边距。然后,您可能仍然会在一个方向上获得一些空白,这是由于画布纵横比与图像纵横比不同。但是,我认为您不想更改图像方面,因此实际上需要剩余的边距。

    【讨论】:

    • 谢谢!正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2013-01-07
    • 2019-04-10
    • 2012-07-23
    • 2018-03-22
    相关资源
    最近更新 更多