无法删除 imshow() 图周围的 matplotlib 填充 [英] Can't remove matplotlib's padding around imshow() figure

查看:66
本文介绍了无法删除 imshow() 图周围的 matplotlib 填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将matplotlib嵌入到我的PyQt4 GUI中,而且时间很长.我可以显示图像,但它在我想要删除的内容周围添加了非常厚的填充.这是我正在做的:

I'm embedding matplotlib into my PyQt4 GUI and I'm having a heck of a time. I can get the image to display but it adds a very thick padding around the content that I'd like to remove. Here's what I'm doing:

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中,则会得到以下结果:

If I add this widget to a QDockWidget I get the following result:

正如您所看到的,它在内容周围带有大的白色填充.我似乎无法删除它,我在网上打开的所有内容都集中在保存图像时删除填充,而不是显示.有谁知道如何在显示时删除此填充?预先感谢.

As you can see it renders with a large white padding around the content. I cannot seem to remove this and everything I'm turning up online is focused on removing the padding when saving the image, not displaying. Does anyone know how to remove this padding at display time? Thanks in advance.

推荐答案

您可以使用 subplots_adjust 消除边距.即

You may use subplots_adjust to get rid of the margins. I.e.

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

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

This will tell the figure not to use any margins around its child axes. You may then still get some white space to one direction, which is due to the canvas aspect ratio not being the same as the image aspect. However, I think that you don't want to change the image aspect and so this remaining margin would acutally be desired.

这篇关于无法删除 imshow() 图周围的 matplotlib 填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆