【问题标题】:Relating a canvas to a figure wxPython将画布与图形wxPython相关联
【发布时间】:2016-01-27 07:22:44
【问题描述】:

我想知道在 wxPython FigureCanvasWxAgg 实例中嵌入 matplotlib 图形时如何在 python 中执行以下伪代码:

需要用到以下物品:

  • ---- 可以使用的进口商品----

    导入 wx

    从 matplotlib.figure 导入图

    从 matplotlib.backends.backend_wxagg 导入 FigureCanvasWxAgg 作为 FigureCanvas

    ---------------------- em>---------------------------------

  • main_canvas;

  • shadow_canvas;

  • big_plot [一个 matplotlib 图形实例,其中有一个大图——就像你用 figure.add_subplots(1,1,1) 制作的那样];

  • small_subplots [一个 matplotlib 图形实例,其中包含 2 个子图 -- 你可以使用 figure.add_subplots(2,1,i),其中 1

  • 一个名为 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()

【问题讨论】:

    标签: python user-interface matplotlib wxpython


    【解决方案1】:

    对于任何可能需要它的人,这是我想出的解决方案:

    #!/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()
    

    要更改显示,请单击图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多