I have successfully placed a matplotlib line chart on a wxpython panel, but the margins look too big to me. I want to reduce the margins, so that the chart will expand to the panel (or almost expand). I tried self.fig.tight_layout() but it didn't reduce the margins

import wx
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas

class GraphFrame():
    def __init__(self, parent):
    self.parent = parent
    self.dpi = 100
    self.fig = Figure((3.0, 3.0), dpi=self.dpi)
    self.canvas = FigCanvas(self.parent, -1, self.fig)
    self.axes = self.fig.add_subplot(111)
    self.fig.tight_layout()
    self.canvas.draw()

chart_panel = wx.Panel(self.main_Frame, -1)
chart = GraphFrame(chart_panel)
有帮助吗?

解决方案

After seeing tight_layout() already in your code, I did a complete re-write of my answer. I hope this is the behavior you want. Somehow tight_layout kind of works in this case (in your snippet tight_layout did not change margina at all on my windows XP box). Probably wx.Frame plays better with FigureCanvasWxAgg than wx.Panel?

import wx
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg

class GraphFrame(wx.Frame):
    def __init__(self, parent):

        wx.Frame.__init__(self, parent, -1, size=(300,300))
        figure = Figure()
        fc = FigureCanvasWxAgg(self, -1, figure)
        axes = figure.add_subplot(111)
        figure.tight_layout()
        self.Show(True)

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    graphframe = GraphFrame(None)
    app.MainLoop()

其他提示

You may be able to use subplots_adjust method

as the following example

fig.subplots_adjust(left=0.1,right=0.9,bottom=0.1,top=0.9)

hope it helps

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top