2013-11-21 68 views
1

我有同樣的問題,如​​。
它有效,但這個數字不能覆蓋整個框架;所以我改變了他的面板的大小和位置。現在的問題是,我的身材仍然和我的身材一樣大,因此我只改變了面板尺寸,我的身材被「剪裁」了。我該如何解決這個問題?
爲了更好地理解這個例子:我的框架是800x600,在這個框架內我的圖形只有400x300的尺寸。嵌入matplotlib圖並改變其大小

編輯:

import wx 
from numpy import arange, sin, pi 
import matplotlib 
#matplotlib.use('WXAgg') 

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas 
from matplotlib.backends.backend_wx import NavigationToolbar2Wx 
from matplotlib.figure import Figure 

class MainFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, 
          "Pyramid App",size=(800,600),pos=((wx.DisplaySize()[0]-800)/2,(wx.DisplaySize()[1]-600)/2),style= wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX) 

     self.PageThree = pageThree(self) 

class pageThree(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent=parent,size=(800,525)) 
     self.myparent=parent 
     self.pageThree=wx.Panel(self,size=(800,525)) # put some elements at this panel 
     self.pylabfigure = wx.Panel(self,size=(440,420),pos=(350,105)) # this is the panel for my figure 
     self.drawPylabFigure() 
     self.draw() 

    def drawPylabFigure(self): 
     self.figure = Figure() 
     self.axes = self.figure.add_subplot(111) 
     self.canvas = FigureCanvas(self.pylabfigure, -1, self.figure) 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) 
     self.SetSizer(self.sizer) 
     self.Fit() 

    def draw(self): 
     #example drawing 
     t = arange(0.0, 3.0, 0.01) 
     s = sin(2 * pi * t) 
     self.axes.plot(t, s) 

if __name__ == "__main__": 
    app = wx.App() 
    MainFrame().Show() 
    app.MainLoop() 
+0

pls.show一些代碼 – joaquin

+0

對不起,我編輯。 – Munchkin

回答

1

這會給你一個如何去做的想法。 重要的是要考慮它把你的面板/帆布由一個sizer組織。另外,如果畫布必須填滿他的身體,則不需要將其放在面板中。

import wx 
from numpy import arange, sin, pi 
import matplotlib 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FCW 
from matplotlib.figure import Figure 

class MainFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, 
          "Pyramid App",size=(800,600), 
          pos=((wx.DisplaySize()[0]-800)/2,(wx.DisplaySize()[1]-600)/2), 
          style=wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX) 

     self.PageThree = pageThree(self) 

class pageThree(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent=parent,size=(800,525)) 
     self.myparent=parent 
     self.pageThree=wx.Panel(self, size=(500,525)) 
     self.drawPyFigure() 
     self.draw() 

    def drawPyFigure(self): 
     self.figure = Figure() 
     self.axes = self.figure.add_subplot(111) 
     self.canvas = FCW(self, -1, self.figure) 
     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     sizer.Add(self.pageThree, 0) 
     sizer.Add(self.canvas, 1, wx.EXPAND) 
     self.SetSizer(sizer) 
     self.Fit() 

    def draw(self): 
     t = arange(0.0, 3.0, 0.01) 
     s = sin(2 * pi * t) 
     self.axes.plot(t, s) 

if __name__ == "__main__": 
    app = wx.App() 
    MainFrame().Show() 
    app.MainLoop() 

enter image description here

0

簡而言之,你要self.figure = Figure(figsize=(400/80.0, 300/80.0))

您需要指定圖形的大小(默認爲8英寸寬×6英寸高)和/或圖形的dpi(默認爲80,用於屏幕顯示)。

但是,請注意,圖屏幕上的圖的大小僅取決於像素的總數(換句話說,即width_inches*dpi x height_inches*dpi)。因此,如果我們想要一個數字是一個特定數量的像素,我們需要將其轉換爲「英寸」(如果它顯示在屏幕上,則與數字的顯示大小無關)由圖的dpi。

+0

你測試過了嗎?當我更改這條線時,沒有任何變化。我用幾個參數對它進行了測試,但是我的圖像看起來總是一樣的。 – Munchkin