代码之家  ›  专栏  ›  技术社区  ›  Mia Chen

在Glad/GTK3滚动窗口中嵌入Matplotlib不会显示maplotlib

  •  1
  • Mia Chen  · 技术社区  · 8 年前

    使用的系统:Windows 7 蟒蛇:2.7.12 填料函:3.14.2 使用的库:matplotlib,networkx

    你好 我试图在GTK上显示嵌入matplotlib的图形。滚动窗口上的显示不起作用。然而,当我试图用工具栏保存它时,我实际上可以保存它。

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    from figure1 import figure
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as          FigureCanvas
    from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
    
    class main_function():
    
    def __init__(self):
        interface= Gtk.Builder()
        interface.add_from_file("interface1.glade")   
        interface.connect_signals(self)
              # relier les signaux aux fonctions        
        # on stocke notre fichier Glade dans des variables
        self.mainWindow=interface.get_object("mainWindow")
        self.aboutchronomap=interface.get_object("aboutchronomap")
        self.fichierdialogue=interface.get_object("fichierdialogue")
        self.sw=interface.get_object("mainFigure")
        self.sw2=interface.get_object("MatplotlibToolbar")
       # on stocke les widgets avec les interfaces
        canvas = FigureCanvas(figure())
        print(type(canvas))
        print(type(figure()))
        self.sw.add_with_viewport(canvas)
        print("self.sw.add_with_viewport(canvas)")        
        toolbar = NavigationToolbar(canvas, self.mainWindow)
        self.sw2.add_with_viewport(toolbar)   
        print("toolbar display")
    
    
    main_function()
    Gtk.main() 
    

    图1.py代码:

    from gi.repository import Gtk
    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure
    from numpy import sin, cos, pi, linspace
    
    
    def figure():
        fig = Figure(figsize=(5,5), dpi=80)
        ax = fig.add_subplot(111)
    
        n = 1000
        xsin = linspace(-pi, pi, n, endpoint=True)
        xcos = linspace(-pi, pi, n, endpoint=True)
        ysin = sin(xsin)
        ycos = cos(xcos)
    
        sinwave = ax.plot(xsin, ysin, color='black', label='sin(x)')
        coswave = ax.plot(xcos, ycos, color='black', label='cos(x)', linestyle='--')
    
        ax.set_xlim(-pi,pi)
        ax.set_ylim(-1.2,1.2)
    
        ax.fill_between(xsin, 0, ysin, (ysin - 1) > -1, color='blue', alpha=.3)
        ax.fill_between(xsin, 0, ysin, (ysin - 1) < -1, color='red',  alpha=.3)
        ax.fill_between(xcos, 0, ycos, (ycos - 1) > -1, color='blue', alpha=.3)
        ax.fill_between(xcos, 0, ycos, (ycos - 1) < -1, color='red',  alpha=.3)
    
        ax.legend(loc='upper left')
    
        ax = fig.gca()
        ax.spines['right'].set_color('none')
        ax.spines['top'].set_color('none')
        ax.xaxis.set_ticks_position('bottom')
        ax.spines['bottom'].set_position(('data',0))
        ax.yaxis.set_ticks_position('left')
        ax.spines['left'].set_position(('data',0))
        return fig
    

    图形通常应显示的滚动窗口的Glade xml代码:

    <child>
                      <object class="GtkScrolledWindow" id="mainFigure">
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="hscrollbar_policy">always</property>
                        <property name="vscrollbar_policy">always</property>
                        <property name="shadow_type">in</property>
                        <child>
                          <placeholder/>
                        </child>
                      </object>
                      <packing>
                        <property name="expand">True</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                      </packing>
    </child>
    

    我的显示器:

    enter image description here

    如果我使用工具栏上的“保存”按钮,我实际上得到了正确的数字: enter image description here

    在GTK上,我看不到它。 有人能帮我吗?

    非常感谢。 米娅

    1 回复  |  直到 8 年前
        1
  •  0
  •   Mia Chen    8 年前

    我设法自己解决了。您应该只添加self.mainWindow。show_all(),这很有效。

    这个问题还有一些其他原因。

    为了在GTK GUI中嵌入matplotlib图片,我使用以下代码:

        def on_carto_display_button_press_event(self,widget,event):
        if self.View.display_Mode != "carto":
            self.View.display_Mode = "carto"
            child=self.View.sw.get_child()
            child1 = self.View.toolbar.get_child()
            #print(child1)
    
            if child != None:
                self.View.toolbar.remove(child1)
                self.View.sw.remove(child)
                self.box.remove(self.View.canvasChrono)
    
            self.box=Gtk.Box()
            self.View.sw.add(self.box)
            self.box.pack_start(self.View.canvasCarto, True, True, 0)
            #Add toolbar
            toolbar = NavigationToolbar(self.View.canvasCarto, self.View.window)
            self.View.toolbar.add_with_viewport(toolbar)
            child1 = self.View.toolbar.get_child()
            #print(child1)
            self.View.sw.show_all()
            #self.View.toolbar.show_all()
    

    #!/usr/bin/python3
    
    from gi.repository import Gtk
    
    from matplotlib.figure import Figure
    from numpy import sin, cos, pi, linspace
    #Possibly this rendering backend is broken currently
    #from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
    from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas    from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
    
    class Signals:
        def on_window1_destroy(self, widget):
            Gtk.main_quit()
    
    builder = Gtk.Builder()
    builder.add_objects_from_file('mpl-ntb-glade.glade', ('window1', '') )
    builder.connect_signals(Signals())
    
    myfirstwindow = builder.get_object('window1')
    sw = builder.get_object('scrolledwindow1')
    sw2 = builder.get_object('scrolledwindow2')
    
    fig = Figure(figsize=(5,5), dpi=80)
    ax = fig.add_subplot(111)
    
    n = 1000
    xsin = linspace(-pi, pi, n, endpoint=True)
    xcos = linspace(-pi, pi, n, endpoint=True)
    ysin = sin(xsin)
    ycos = cos(xcos)
    
    sinwave = ax.plot(xsin, ysin, color='black', label='sin(x)')
    coswave = ax.plot(xcos, ycos, color='black', label='cos(x)', linestyle='--')
    
    ax.set_xlim(-pi,pi)
    ax.set_ylim(-1.2,1.2)
    
    ax.fill_between(xsin, 0, ysin, (ysin - 1) > -1, color='blue', alpha=.3)
    ax.fill_between(xsin, 0, ysin, (ysin - 1) < -1, color='red',  alpha=.3)
    ax.fill_between(xcos, 0, ycos, (ycos - 1) > -1, color='blue', alpha=.3)
    ax.fill_between(xcos, 0, ycos, (ycos - 1) < -1, color='red',  alpha=.3)
    
    ax.legend(loc='upper left')
    
    ax = fig.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data',0))
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data',0))
    
    fig.tight_layout()
    
    canvas = FigureCanvas(fig)
    sw.add_with_viewport(canvas)
    
    toolbar = NavigationToolbar(canvas, myfirstwindow)
    sw2.add_with_viewport(toolbar)
    
    myfirstwindow.show_all()
    Gtk.main()

    此文档对帮助您解决问题非常有用=)

    http://gtk3-matplotlib-cookbook.readthedocs.io/en/latest/matplotlib-toolbar.html#matplotlib-toolbar-with-glade