开发者

How do I draw a colorbar in a gtk/glade window embedded matplotlib figure?

I am currently working on a gtk/python front-end to a simulation tool. I've got everything working as planned, with one exception- I can't figure out how to draw the colorbar associated with the plot I am displaying. I can get the colorbar to display just fine if I don't embed my plot in a GTK window, but as soon as I embed it, the colorbar disappears.

The relevant rendering code (within my python gui object) is below (the init and render_domain class functions). I am trying to render the colorbar, 3-lines up from the bottom of the render_domain function. The colormap is working fine (my data in the mesh is properly colored), it is just getting the actual colorbar to appear that is the issue. 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.builde开发者_C百科r.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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜