如何在wx框架中将matplotlib画布与wx面板对齐

问题描述 投票:0回答:2

我正在尝试制作一个GUI,在该GUI的左侧为matplotlib.backends.backend_wxagg.FigureCanvasWxAgg,在右侧为wx.Panel。在wx.Frame中对齐它们的正确方法是什么?

代码:

import wx
import matplotlib.figure
import matplotlib.backends.backend_wxagg

class gui:
    def __init__(self):
        self.app = wx.App()

        self.app.frame = wx.Frame(parent=None, title="GUI")
        self.app.frame.SetSize(1024, 768)
        self.app.frame.Centre()

        figure = matplotlib.figure.Figure()
        subplots = figure.subplots(nrows=2, ncols=2)
        subplots[0,0].set_title("Plot 1")
        subplots[0,1].set_title("Plot 2")
        subplots[1,0].set_title("Plot 3")
        subplots[1,1].set_title("Plot 4")
        self.app.frame.canvas = matplotlib.backends.backend_wxagg.FigureCanvasWxAgg(
                                parent=self.app.frame,
                                id=-1,
                                figure=figure)

        self.app.frame.panel = wx.Panel(parent=self.app.frame)

        self.app.frame.sizer = wx.FlexGridSizer(cols=2)
        self.app.frame.sizer.Add(self.app.frame.canvas, wx.SizerFlags().Left())
        self.app.frame.sizer.Add(self.app.frame.panel, wx.SizerFlags().Right())
        self.app.frame.SetSizer(self.app.frame.sizer)

        self.app.frame.Show()
        self.app.MainLoop()

结果:

似乎matplotlib.backends.backend_wxagg.FigureCanvasWxAggwx.Panel重叠,而不是与其对齐。

python matplotlib wxpython
2个回答
0
投票

使用.set_position设置.set_position对象的大小和位置。matplotlib.axes.Axes的参数是[0.0,1.0]范围内的浮点值,并分别指定相对位置大小:

matplotlib.axes.Axes

.set_position


0
投票

我找到了解决问题的方法。由于这是我第一次使用Python和wxPython,所以我不知道如果仅创建subplots = figure.subplots(nrows=2, ncols=2) scale = 0.35 subplots[0,0].set_position(pos = [0.10, 0.57, scale, scale], which='both') subplots[1,0].set_position(pos = [0.10, 0.1, scale, scale], which='both') subplots[0,1].set_position(pos = [0.57, 0.57, scale, scale], which='both') subplots[1,1].set_position(pos = [0.57, 0.1, scale, scale], which='both') 但不添加任何内容,什么也不会出现。因此,我以为隐藏在图形的后面,但实际上它是空的。在面板上添加了一些项目之后,一切都按预期进行。新代码:

wx.panel

新结果:wx.panel

© www.soinside.com 2019 - 2024. All rights reserved.