How to draw color bar in gtk / glade window enabled by matplotlib?

I am currently working on a gtk / python interface for a modeling tool. I have everything that was planned, with one exception - I can’t figure out how to draw a color panel related to the plot that I am showing. I can get a color panel to display it just fine, if I don’t paste my story into the GTK window, but as soon as I embed it, the color screen disappears.

The corresponding rendering code (inside my guyth python object) is below ( init and render_domain functions). I am trying to display a colorbar, 3 lines from the bottom of the render_domain function. The color palette works fine (my data in the grid is properly colored), the actual color panel just appears, which is the problem. Thoughts?

class AppGui(object):

def __init__(self):
    #### --- SET UP GTK INTERFACE FROM GLADE FILE --- ####
    gladefile = "py_FD2D.glade"
    self.windowname = "main_window"
    self.builder = gtk.Builder()
    self.builder.add_from_file(gladefile)

    #### --- SET UP GUI SIGNALS --- ####
    self.window = self.builder.get_object(self.windowname)
    dic = {
        "on_main_window_destroy" : self.quit,
        "on_run_simulation_button_clicked" : self.run_simulation,
        "on_play_button_clicked" : self.play_animation,
        "on_play_slider_value_changed" : self.slider_changed,
        "on_cap_pres_yn_changed" : self.cap_pres_menu_changed,
    }
    self.builder.connect_signals(dic)

    #### --- SET UP FIGURE IN GTK WINDOW --- ####
    self.figure = matplotlib.figure.Figure(figsize=(6,4), dpi=72)
    self.axis = self.figure.add_subplot(1,1,1)

    self.canvas = FigureCanvas(self.figure)
    self.canvas.show()
    self.toolbar = NavigationToolbar(self.canvas, self.window)

    # Place the figure in the plot_holder pane of the py_FD2D.glade file
    self.graphview = self.builder.get_object("plot_holder")
    self.graphview.pack_start(self.canvas, True, True)
    self.graphview.pack_start(self.toolbar, False, False)

    self.FD = gcsmt.FD2D()  #instantiate the simulator
    self.update_all()       #update all simulation parameters from gtk GUI fields
    self.render_domain()    #render the initial state of the plot

    self.builder.get_object(self.windowname).show()
    self.stop_sim = False

def render_domain(self):
    self.axis.clear()

    self.axis.set_xlabel('Formation Width (meters)')
    self.axis.set_ylabel('Formation Thickness (meters)')
    self.axis.set_title('Formation Saturation')

    self.msh = matplotlib.collections.QuadMesh(self.x_cells, self.y_cells, self.verts, True)

    if self.FD.HasRun()==False:
        self.msh.set_array(np.zeros(self.x_cells*self.y_cells))
    else:
        self.msh.set_array(np.array(self.FD.GetTimestepData(0)))

    self.msh.set_clim(0.0, 1.0)
    self.cax = self.axis.add_collection(self.msh)
    self.axis.axis([0, self.x_max, 0, self.y_top])
    plt.colorbar(self.cax)  ###!!! I have tried self.cax and self.msh, and various combos
    self.toolbar.show()
    self.canvas.draw()
+3
source share

All Articles