wxPython无法正确定位matplotlib块

时间:2014-06-18 15:13:18

标签: python-2.7 user-interface matplotlib wxpython

我已经使用代码在图像格式here上绘制一个矩形,并对其进行了一些修改以适应我的目的。我唯一的问题是,即使我制作fr.ShowFullScreen(True),它也不会全屏显示。图像周围有灰色区域。 编辑后的代码如下(矩形部分不相关):

# Use the wxPython backend of matplotlib
import matplotlib       
matplotlib.use('WXAgg')

# Matplotlib elements used to draw the bounding rectangle
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure

# wxPython stuff for the demo
import wx

class RectangleSelectImagePanel(wx.Panel):
    def __init__(self, parent, pathToImage=None):

        # Initialise the parent
        wx.Panel.__init__(self, parent)

        # Intitialise the matplotlib figure
        self.figure = Figure(figsize=(20,15), dpi=80)

        # Create an axes, turn off the labels and add them to the figure
        self.axes = self.figure.add_subplot(1, 1, 1)
        self.axes.set_axis_off() 

        # Add the figure to the wxFigureCanvas
        self.canvas = FigureCanvas(self, -1, self.figure)

        # Sizer to contain the canvas
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 0, wx.ALL)
        self.SetSizer(self.sizer)
        self.Fit()

    def setImage(self, pathToImage):
        '''Sets the background image of the canvas'''

        import matplotlib.image as mpimg

        # Load the image into matplotlib
        image = matplotlib.image.imread(pathToImage)
        # left in as anchor for image size
        self.imageSize = image.shape[0:2]
        print self.imageSize
# 
        # Add the image to the figure and redraw the canvas. Also ensure the aspect ratio of the image is retained.
        self.axes.imshow(image, interpolation="quadric", aspect='auto')
        self.canvas.draw()

if __name__ == "__main__":

    # Create an demo application
    app = wx.App()

    # Create a frame and a RectangleSelectorPanel
    fr = wx.Frame(None, title='test')
    panel = RectangleSelectImagePanel(fr)

    # Set the image in the panel
    from matplotlib.cbook import get_sample_data
    imgpath = get_sample_data('logo2.png', asfileobj=False)
    panel.setImage(imgpath)

    # Start the demo app
    fr.ShowFullScreen(True)
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

你可能需要的是subplots_adjust。

from pylab import *
from matplotlib.cbook import get_sample_data
imgpath = get_sample_data('logo2.png', asfileobj=False)
image = matplotlib.image.imread(imgpath)

imshow(image, aspect='auto')
subplots_adjust(left=0, right=1, top=1, bottom=0, wspace=0, hspace=0)

这将生成图像周围没有边距的图。

所以,如果你把

self.figure.subplots_adjust(left=0, right=1, top=1, bottom=0, wspace=0, hspace=0)

在imshow line之后,你会得到更接近你需要的东西。

然后,您可能还需要调整figsize参数。对于我的1920 x 1080显示器,我需要1920.0 / 80英寸×1080.0 / 80英寸(24,13.5)