python - Relating a canvas to a figure wxPython -


i know how perform following pseudocode in python when embedding matplotlib figure inside of wxpython figurecanvaswxagg instance:

the following items need used:

  • ---- imports can used ----

    import wx

    from matplotlib.figure import figure

    from matplotlib.backends.backend_wxagg import figurecanvaswxagg figurecanvas

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

  • main_canvas;

  • shadow_canvas;

  • big_plot [a matplotlib figure instance 1 big plot in -- 1 make figure.add_subplots(1,1,1)];

  • small_subplots [a matplotlib figure instance with, say, 2 subplots in -- make figure.add_subplots(2,1,i), 1<=i<=2]

  • a function called swapview(main_canvas,shadow_canvas,big_plot,small_subplots) swaps figure in shadow_canvas 1 in main_canvas (so keep switching between 1 big plot , 1 many small plots)

  • a function updatedisplay() dynamically updates display every time call swapview()

     ******* pseudocode *******  main_canvas.show()  shadow_canvas.hide()  main_canvas has big_plot  shadow_canvas has small_subplots   if big_plot in main_canvas:       swapview(...) ---> should put big_plot in shadow_canvas , small_subplots in main_canvas  else:       swapview(...) ---> should put small_subplots in shadow_canvas , big_plot in main_canvas  updatedisplay()  ******* end of code ******* 

here initial attempt @ code , unfortunately can't find way find figure 1 displayed.

#!/usr/bin/python  # -*- coding: utf-8 -*-  import numpy np import wx import time matplotlib.figure import figure matplotlib.backends.backend_wxagg import figurecanvaswxagg 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 override canvas1, when canvas2 hides canvas1 should show         canvas2.show()         canvas1.show()         self.show()         print "starting swap displays!"         time.sleep(1)         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 main_canvas shadow_canvas show either fig1 or fig2.              initially, big_plot has main_canvas , small_plots have shadow_canvas         '''         wx.yield()         print list(main_canvas)         print list(big_plot.get_children())         time.sleep(2)         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'          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() 

for might need it, here's solution came with:

#!/usr/bin/python  # -*- coding: utf-8 -*-  import numpy np import wx import time matplotlib.figure import figure matplotlib.backends.backend_wxagg import figurecanvaswxagg 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 detection of mouseclicks plots in plotting window         print "click anywhere on figure swap plots!"         self.display = 1      def onloadfigure(self,event = none):         print "tried 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 main_canvas shadow_canvas show either fig1 or fig2.              initially, big_plot has main_canvas , small_plots have shadow_canvas         '''         wx.yield()         print display         if display == 1: #show big plot             print 'big_plot showing'             big_plot.set_canvas(main_canvas)             main_canvas.show()             time.sleep(0.01) #fastest time 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() 

to make display change, click on figure.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -