views:

74

answers:

0

I want to draw two plots describing the color distribution of the dynamic image. I tried to build all these three share the same axes but failed. And the plot flashes not displays continuously. Thanks.

import wx
import sys, time, os, gc
import wx.lib.colourselect as cs
import matplotlib.cm as cm

import numpy as npy
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure

TIMER_ID = wx.NewId()
cmapList = ['jet', 'hot', 'cool', 'spectral']

# Class TopPanel creates a Text display
class TopPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        font = wx.Font(20, wx.NORMAL, wx.NORMAL, wx.BOLD)
        self.st = wx.StaticText(self, -1, "Pseudo Image", (5,10), style=wx.ALIGN_CENTRE)
        self.st.SetFont(font)

        self.SetForegroundColour((164, 211, 238)) # lightskyblue2

# Class PlotPanel creates a Image display and intensity distribution
class PlotPanel(wx.Panel):    
    def __init__( self, parent, cmap=cm.jet, dpi=None, xpix=100, ypix=100, **kwargs):
        # initialize Panel
        if 'id' not in kwargs.keys():
            kwargs['id'] = wx.ID_ANY
        if 'style' not in kwargs.keys():
            kwargs['style'] = wx.NO_FULL_REPAINT_ON_RESIZE
        wx.Panel.__init__( self, parent, **kwargs )

        # initialize matplotlib stuff
        self.figure = Figure((10,8), 80)
        self.canvas = FigureCanvasWxAgg( self, -1, self.figure )

        rect = [0.065,0.065,0.85,0.85] 
        self.ax = self.figure.add_axes(rect)
        self.ax.xlim = ypix
        self.ax.ylim = xpix
        self.ax.set_xlim([0, self.ax.xlim])
        self.ax.set_ylim([self.ax.ylim, 0])
        self.cmap = cmap

        self.axHistx = self.ax.twinx()
        self.axHisty = self.ax.twiny()

        self.Bind(wx.EVT_TIMER, self.OnTimer)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    def InitPlot(self):
        self.x = npy.empty((self.ax.xlim,self.ax.ylim))
        self.x.flat = npy.arange(self.ax.ylim)*2*npy.pi/float(self.ax.ylim)
        self.y = npy.empty((self.ax.ylim,self.ax.xlim))
        self.y.flat = npy.arange(self.ax.xlim)*2*npy.pi*5.0/(self.ax.xlim*4.0)
        self.y = npy.transpose(self.y)
        z = npy.fabs(npy.sin(self.x)/2. + npy.cos(self.y)/2.)
        self.im = self.ax.imshow( z, cmap=self.cmap)#, interpolation='nearest')

        self.im.set_array(z)
        self.canvas.draw()

        self.binwidth = 1
        self.xbins = npy.arange(0, self.ax.ylim, self.binwidth)
        self.ybins = npy.arange(0, self.ax.xlim, self.binwidth)

        self.xline, = self.axHistx.plot([], [], animated=True, lw=2)
        self.yline, = self.axHisty.plot([], [], animated=True, lw=2)
        self.xline.set_xdata(self.xbins)
        self.yline.set_ydata(self.ybins)

        self.axHistx.set_ylim([0,3])
        self.axHisty.set_xlim([0,4])

        self.z = npy.zeros([self.ax.xlim, self.ax.ylim], float)

        self.background = None

        self.xproject = [0.]*self.ax.ylim
        self.yproject = [0.]*self.ax.xlim

        self.timer = wx.Timer(self, TIMER_ID)
    #    self.Bind(wx.EVT_TIMER, self.OnTimer,self.timer)
     #   wx.EVT_TIMER(self, TIMER_ID, self.OnTimer)
        self.timer.Start(1000)

    def compute(self):

        zt = npy.transpose(self.z)
        for i in range(0, self.ax.ylim):
            for j in range (0, self.ax.xlim):
                self.xproject[i] += zt[i][j]  
            self.xproject[i] /= float(self.ax.xlim)

     # plot intensity distribution on Y axis

        for i in range(0, self.ax.xlim):
            for j in range (0, self.ax.ylim):
                self.yproject[i] += self.z[i][j]  
            self.yproject[i] /= float(self.ax.ylim)

    def update_line(self):
     #   self.figure.canvas.restore_region(self.background)    
     #   if self.drawLine == False:
      #      return 

        self.compute()

  #      self.xline.set_xdata(self.xbins)

        self.xline.set_ydata(self.xproject)
        self.axHistx.draw_artist(self.xline)
        self.canvas.blit(self.axHistx.get_figure().bbox)


        self.yline.set_xdata(self.yproject)
        self.axHisty.draw_artist(self.yline)
        self.canvas.blit(self.axHisty.get_figure().bbox)


    def OnTimer(self, evt):
        self.x += npy.pi/15
        self.y += npy.pi/20
        self.z = npy.fabs(npy.sin(self.x) + npy.cos(self.y))
        self.im = self.ax.imshow( self.z, cmap=self.cmap)

        self.im.set_array(self.z)
        self.OnEraseBackground(evt)

        self.update_line()

        self.canvas.draw()

    def OnEraseBackground(self, evt):
        dc = wx.ClientDC(self)
        rect = self.GetUpdateRegion().GetBox()
        dc.SetClippingRect(rect)

    def SetCmap(self, value):
        self.cmap = cmapList[value]


class InfoPanel(wx.Panel):
    def __init__(self, parent, callback):
        wx.Panel.__init__(self, parent)
        font = wx.Font(20, wx.NORMAL, wx.NORMAL, wx.BOLD)
        vbox = wx.BoxSizer(wx.VERTICAL)

        self.callback = callback
        self.xpix = 100

        xlabel = wx.StaticText(self, -1, 'X Pixels:', (20, -1))
        xtext = wx.TextCtrl(self, -1, "400", style=wx.TE_READONLY|wx.NO_BORDER, size=(50,-1))
        xtext.SetBackgroundColour("gray")
        xedit = wx.TextCtrl(self, -1, "", size=(50, -1))
 #       self.Bind(wx.EVT_TEXT, self.OnText, xtext)


        ylabel = wx.StaticText(self, -1, 'Y Pixels:', (20, -1))
        ytext = wx.TextCtrl(self, -1, "400", size=(50,-1), style=wx.TE_READONLY|wx.NO_BORDER)
        ytext.SetBackgroundColour("gray")
        yedit = wx.TextCtrl(self, -1, "", size=(50,-1))


        cmaplabel = wx.StaticText(self, -1, 'C Map', (20, -1))

        cb = wx.ComboBox(self, -1, "jet", (90, 80), (120, -1), cmapList, style=wx.CB_DROPDOWN)

        tsizer = wx.FlexGridSizer(cols=3, hgap=6, vgap=6)
        tsizer.AddMany([xlabel, xtext, xedit, ylabel, ytext, yedit, cmaplabel, cb])
#        tsizer.Add(cb)

        vbox.Add(tsizer, 9, wx.ALL)
        self.SetSizer(vbox)
        self.Bind(wx.EVT_COMBOBOX, self.OnSelect, cb)

    def OnSelect(self, evt):
        cmap = evt.GetSelection()
        self.callback(cmap)  

  #  def OnText(self, evt):
   #     self.xpix = evt.GetValue()
    #    print self.xpix






class MyFrame(wx.Frame):

    def __init__(self):


        wx.Frame.__init__(self, None, title="Test", size=(1200,1000))

        self.top_panel = TopPanel(self)
        self.plt_panel = PlotPanel(self)
        self.side_panel = InfoPanel(self, self.OnSelect)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.top_panel, 0, wx.ALIGN_CENTRE|wx.TOP, 10)

        downSizer= wx.BoxSizer(wx.HORIZONTAL)        
        downSizer.Add(self.plt_panel, 0, wx.EXPAND, 20)
        downSizer.Add(self.side_panel, 1, wx.RIGHT, 20)

        mainSizer.Add(downSizer, 0, wx.BOTTOM)
        self.SetSizer(mainSizer)

     #   self.timer = wx.Timer(self, TIMER_ID)
    #    self.Bind(wx.EVT_TIMER, self.OnTimer,self.timer)

        mainSizer.Fit(self)
        self.plt_panel.InitPlot()


     #   self.timer = wx.Timer(self, TIMER_ID)
    #    self.Bind(wx.EVT_TIMER, self.OnTimer,self.timer)
     #   wx.EVT_TIMER(self, TIMER_ID, self.OnTimer)
      #  self.timer.Start(100)

    def OnSelect(self, value):
        self.plt_panel.SetCmap(value)





  #  def OnTimer(self, evt):
   #     self.plt_panel.OnTimer(evt)
  #      pass


app = wx.App(False)
frame = MyFrame()
frame.Show()
app.MainLoop()