2016-01-27 12 views
-1

我想知道如何在Python中嵌入一個wxPython的FigureCanvasWxAgg實例的內部matplotlib人物時可以執行下列僞代碼:與畫布到數字的wxPython

以下項目需要使用:

  • - - - - 可以採用進口- - - -

    進口WX

    從matplotlib.figure導入圖

    從matplotlib.backends.backend_wxagg進口FigureCanvasWxAgg作爲FigureCanvas

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  • main_canvas;

  • shadow_canvas;

  • big_plot [一個matplotlib圖形實例,其中有一個大的陰謀 - 就像你會用figure.add_subplots(1,1,1)]所做的那樣;

  • small_subplots [有,比如說一個matplotlib圖實例2個次要情節在裏面 - 你會作出與人物。add_subplots(2,1,i),其中1 < = I < = 2]

  • 被叫SwapView(main_canvas,shadow_canvas,big_plot,small_subplots)函數基本上交換圖中當前正在shadow_canvas與一個在main_canvas(所以保持一個有一個大陰謀和一個有許多小地塊之間切換))

  • 功能UpdateDisplay(即每次調用SwapView時間()動態地更新顯示

    ******* PSEUDOCODE ******* 
    main_canvas.show() 
    shadow_canvas.hide() 
    main_canvas has big_plot initially 
    shadow_canvas has small_subplots initially 
    
    if big_plot in main_canvas: 
         SwapView(...) ---> should put big_plot in shadow_canvas and small_subplots in the main_canvas 
    else: 
         SwapView(...) ---> should put the small_subplots in shadow_canvas and the big_plot in main_canvas 
    UpdateDisplay() 
    ******* END OF CODE ******* 
    

這是我在這段代碼的初始嘗試,不幸的是我找不到找到當前顯示哪個圖的方法。

#!/usr/bin/python 

# -*- coding: utf-8 -*- 

import numpy as np 
import wx 
import time 
from matplotlib.figure import Figure 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas 

class myframe(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self,parent = None, id = -1, title = 'LoadFigure()', size = (800,800)) 

     self.figurePanel = FigurePanel(parent = self) 

     canvas1 = self.figurePanel.canvas 
     canvas2 = self.figurePanel.enlarged_canvas 

     fig1 = self.figurePanel.enlarged_figure 
     fig2 = self.figurePanel.figure 

     fig1.set_canvas(canvas1) #enlarged_fig resides in canvas1 
     fig2.set_canvas(canvas2) #fig resides in canvas2 

     #Show both canvases ---> canvas2 will override canvas1, but when canvas2 hides canvas1 should show 
     canvas2.Show() 
     canvas1.Show() 
     self.Show() 
     print "Starting to swap displays!" 
     time.sleep(1) 
     for i in range(10): 
      print "run: %d"%i 
      self.SwapView(big_plot = fig1,small_plots = fig2,main_canvas = canvas1,shadow_canvas = canvas2) 
      time.sleep(1) 

    def SwapView(self,big_plot,small_plots,main_canvas,shadow_canvas): 
     ''' 
      Keep swapping the main_canvas with the shadow_canvas to show either fig1 or fig2. 

      Initially, big_plot has main_canvas and small_plots have shadow_canvas 
     ''' 
     wx.Yield() 
     print list(main_canvas) 
     print list(big_plot.get_children()) 
     time.sleep(2) 
     for child in big_plot.get_children(): 
      if child == main_canvas: 
       print 'big_plot has main_canvas' 
       big_plot.set_canvas(shadow_canvas) 
       small_plots.set_canvas(main_canvas) 
       main_canvas.draw() 
       wx.Yield() 
       main_canvas.Show() 
      else: 
       print 'big_plot has shadow_canvas' 

     for child in small_plots.get_children(): 
      if child == main_canvas: 
       print 'small_plots has main_canvas' 
       small_plots.set_canvas(shadow_canvas) 
       big_plot.set_canvas(main_canvas) 
       main_canvas.draw() 
       wx.Yield() 
       main_canvas.Show() 
      else: 
       print 'small_plots has shadow_canvas' 

class FigurePanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.figPanel = self 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.figure = Figure(figsize = (8,6.1), dpi =60) 
     self.ax = self.figure.add_subplot(1,1,1) 
     self.ax.plot([1,2,3],[1,2,3]) 
     self.enlarged_figure = Figure(figsize = (8,6.1), dpi = 60) 
     self.ax1 = self.enlarged_figure.add_subplot(2,1,1) 
     self.ax2 = self.enlarged_figure.add_subplot(2,1,2) 
     self.ax1.plot([1,2,3],[1,4,9]) 
     self.ax2.plot([1,2,3],[1,4,9]) 
     self.canvas = FigureCanvas(self, -1, self.figure) 
     self.enlarged_canvas = FigureCanvas(self,-1,self.enlarged_figure) 
     self.Layout() 
     self.Fit() 

if __name__ == "__main__": 
    app = wx.App(False) 
    fr = myframe() 
    app.MainLoop() 

回答

0

對於任何可能需要的時候,這裏就是我想出瞭解決方案:

#!/usr/bin/python 

# -*- coding: utf-8 -*- 

import numpy as np 
import wx 
import time 
from matplotlib.figure import Figure 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas 

class myframe(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self,parent = None, id = -1, title = 'SWAP!', size = (480,390)) 

     self.figurePanel = FigurePanel(parent = self) 

     self.canvas1 = self.figurePanel.canvas 
     self.canvas2 = self.figurePanel.enlarged_canvas 

     self.fig1 = self.figurePanel.enlarged_figure 
     self.fig2 = self.figurePanel.figure 

     self.fig1.set_canvas(self.canvas1) #enlarged_fig resides in canvas1 

     self.canvas1.Show() 
     self.Show() 

     self.canvas2.mpl_connect("button_release_event",self.OnLoadFigure) #Enable the detection of mouseclicks for the plots in the plotting window 
     print "Click anywhere on the figure to swap the plots!" 
     self.display = 1 

    def OnLoadFigure(self,event = None): 
     print "Tried to load figure" 
     if event != None: 
      self.display = self.SwapView(big_plot = self.fig1 ,small_plots = self.fig2 , display = self.display, main_canvas = self.canvas1 , shadow_canvas = 0) 

    def SwapView(self,big_plot = None,display = -1, small_plots = None,main_canvas = None,shadow_canvas = None): 
     ''' 
      Keep swapping the main_canvas with the shadow_canvas to show either fig1 or fig2. 

      Initially, big_plot has main_canvas and small_plots have shadow_canvas 
     ''' 
     wx.Yield() 
     print display 
     if display == 1: #Show the big plot 
      print 'big_plot showing' 
      big_plot.set_canvas(main_canvas) 
      main_canvas.Show() 
      time.sleep(0.01) #Fastest time you can pick 
      wx.Yield() 
     else: 
      print 'small_plots showing' 
      main_canvas.Hide() 
      wx.Yield() 

     self.Refresh(canvas = main_canvas) 
     display = not(display) 
     return display 

    def Refresh(self,canvas = None,figure = None): 
     wx.Yield() 
     if canvas != None: 
      print "draw" 
      canvas.draw() 
     self.Update() 
     self.figurePanel.Update() 
     wx.Yield() 

class FigurePanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.figPanel = self 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.figure = Figure(figsize = (8,6.1), dpi =60) 
     self.ax = self.figure.add_subplot(1,1,1) 
     self.ax.plot([1,2,3],[1,2,3]) 
     self.enlarged_figure = Figure(figsize = (8,6.1), dpi = 60) 
     self.ax1 = self.enlarged_figure.add_subplot(2,1,1) 
     self.ax2 = self.enlarged_figure.add_subplot(2,1,2) 
     self.ax1.plot([1,2,3],[1,4,9]) 
     self.ax2.plot([1,2,3],[1,4,9]) 
     self.canvas = FigureCanvas(self, -1, self.figure) 
     self.enlarged_canvas = FigureCanvas(self,-1,self.enlarged_figure) 
     self.Layout() 
     self.Fit() 

if __name__ == "__main__": 
    app = wx.App(False) 
    fr = myframe() 
    app.MainLoop() 

要使顯示更改,請單擊上圖。