[Matplotlib-users] Beginner questions about OO interface

David Aldrich David.Aldrich at EMEA.NEC.COM
Mon Nov 16 05:55:02 EST 2015


Hi

I am new to Matplotlib and am struggling a bit to differentiate between the OO and pyplot interfaces.  I'm actually working with the Kivy GUI framework and trying to plot 4 subplots on a single figure, to be displayed by Kivy.  Here's a snippet of my code:

def create_plot(self):

        self.fig, ((self.ax0, self.ax1), (self.ax2, self.ax3)) = plt.subplots(nrows=2, ncols=2)

        self.ax0.set_title("A")
        self.ax0.grid(True, lw = 2, ls = '--', c = '.75')

        self.ax1.set_title("B")
        self.ax1.grid(True, lw = 2, ls = '--', c = '.75')

        self.ax2.set_title("C")
        self.ax2.grid(True, lw = 2, ls = '--', c = '.75')

        self.ax3.set_title("D")
        self.ax3.grid(True, lw = 2, ls = '--', c = '.75')

        #plt.tight_layout()
        plt.show()

        canvas = self.fig.canvas
        self.add_widget(canvas)

What worries me is that I am calling plt methods and assigning the results to my objects.  Is plt the state machine interface and not the OO interface, or is this OK?

Secondly, I want to periodically update the plotted lines, so I have a plot method that does this:

    def plot(self,  xCoords, yCoords):

        if len(self.ax0.lines) > 0:
            self.ax0.lines.pop(0)

        line = self.ax0.plot(xCoords, yCoords, color='blue')

        canvas = self.fig.canvas
        canvas.draw()

Does that look ok?  Can I just pop the existing line, or should I reuse the existing line?

Lastly, and most difficult, if I enable:

plt.tight_layout()

I get an exception:

C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\matplotlib\tight_layout.py:225: UserWarning: tight_layout : falling back to Agg renderer
   warnings.warn("tight_layout : falling back to Agg renderer")
 Traceback (most recent call last):
   File "main.py", line 1117, in <module>
     GuiApp().run()
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\app.py", line 801, in run
     self.load_kv(filename=self.kv_file)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\app.py", line 598, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 1801, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 1880, in load_string
     self._apply_rule(widget, parser.root, parser.root)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2038, in _apply_rule
     self._apply_rule(child, crule, rootrule)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2037, in _apply_rule
     self.apply(child)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 1924, in apply
     self._apply_rule(widget, rule, rule)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2038, in _apply_rule
     self._apply_rule(child, crule, rootrule)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2038, in _apply_rule
     self._apply_rule(child, crule, rootrule)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2035, in _apply_rule
     child = cls(__no_builder=True)
   File "C:\SVNProj\Raggio\trunk\hostconsole\gui\mygraph.py", line 127, in __init__
     self.create_plot()
   File "C:\SVNProj\Raggio\trunk\hostconsole\gui\mygraph.py", line 224, in create_plot
     self.add_widget(canvas)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\uix\boxlayout.py", line 211, in add_widget
     widget.bind(
 AttributeError: 'FigureCanvasAgg' object has no attribute 'bind'

Can anyone help with that please?

Best regards

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20151116/1ec26a6d/attachment.html>


More information about the Matplotlib-users mailing list