Beginner questions about OO interface

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>

Hello David,

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?

Indeed, plt is the state machine interface, and it isn't exactly the same
thing to say "plt.show()" and to show a particular figure. You can call
`self.fig.show()`, though.

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?

That would work, but it is very inefficient. Most matplotlib artist objects
have some sort of "set_data()" or "set_offsets()" method that would let you
update the data contained in the artist. See the following animation
example: http://matplotlib.org/examples/animation/animate_decay.html

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?

tight_layout() isn't the issue here (well, directly). The issue is that the
canvas object that you added as a widget is not a widget as far as Kivy is
concerned. It doesn't subclass anything that Kivy recognizes as a widget.
By its very nature, FigureCanvasAgg is completely independent of any GUI
frameworks. You would need to have selected the appropriate backend for
matplotlib to use prior to importing pyplot (I don't know which one Kivy is
compatible with, GTK? QT? something else?).

By the way, chapter 5 of my book, "Interactive Applications Using
Matplotlib" goes into detail explaining the ins and outs of GUI embedding
with matplotlib. While I don't cover Kivy, I do a Rosetta Stone-like
explanation covering GTK, Qt4, Wx, and Tk, and I explain the general
concepts. Perhaps it might be useful?

http://www.amazon.com/Interactive-Applications-using-Matplotlib-Benjamin/dp/1783988843/

Cheers!
Ben Root

Best regards

David

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

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

···

On Mon, Nov 16, 2015 at 5:55 AM, David Aldrich <David.Aldrich at emea.nec.com> wrote:

There is on-going work to write a mpl backend for kivy:
https://github.com/kivy-garden/garden.matplotlib

···

On Mon, Nov 16, 2015 at 12:23 PM Benjamin Root <ben.v.root at gmail.com> wrote:

Hello David,

On Mon, Nov 16, 2015 at 5:55 AM, David Aldrich <David.Aldrich at emea.nec.com > > wrote:

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?

Indeed, plt is the state machine interface, and it isn't exactly the same
thing to say "plt.show()" and to show a particular figure. You can call
`self.fig.show()`, though.

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?

That would work, but it is very inefficient. Most matplotlib artist
objects have some sort of "set_data()" or "set_offsets()" method that would
let you update the data contained in the artist. See the following
animation example:
http://matplotlib.org/examples/animation/animate_decay.html

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?

tight_layout() isn't the issue here (well, directly). The issue is that
the canvas object that you added as a widget is not a widget as far as Kivy
is concerned. It doesn't subclass anything that Kivy recognizes as a
widget. By its very nature, FigureCanvasAgg is completely independent of
any GUI frameworks. You would need to have selected the appropriate backend
for matplotlib to use prior to importing pyplot (I don't know which one
Kivy is compatible with, GTK? QT? something else?).

By the way, chapter 5 of my book, "Interactive Applications Using
Matplotlib" goes into detail explaining the ins and outs of GUI embedding
with matplotlib. While I don't cover Kivy, I do a Rosetta Stone-like
explanation covering GTK, Qt4, Wx, and Tk, and I explain the general
concepts. Perhaps it might be useful?

http://www.amazon.com/Interactive-Applications-using-Matplotlib-Benjamin/dp/1783988843/

Cheers!
Ben Root

Best regards

David

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________

Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

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

Hi Benjamin

Thanks for your reply. I have looked at your book on Amazon but thought I should go for Sandro Tosi?s book initially, just to learn the basics of Matplotlib. Perhaps I can get yours later ?

Best regards

David

···

From: Benjamin Root [mailto:ben.v.root@gmail.com]
Sent: 16 November 2015 17:23
To: David Aldrich <David.Aldrich at EMEA.NEC.COM>
Cc: matplotlib-users at python.org
Subject: Re: [Matplotlib-users] Beginner questions about OO interface

Hello David,

On Mon, Nov 16, 2015 at 5:55 AM, David Aldrich <David.Aldrich at emea.nec.com<mailto:David.Aldrich at emea.nec.com>> wrote:
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?

Indeed, plt is the state machine interface, and it isn't exactly the same thing to say "plt.show()" and to show a particular figure. You can call `self.fig.show()`, though.

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?

That would work, but it is very inefficient. Most matplotlib artist objects have some sort of "set_data()" or "set_offsets()" method that would let you update the data contained in the artist. See the following animation example: http://matplotlib.org/examples/animation/animate_decay.html

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?

tight_layout() isn't the issue here (well, directly). The issue is that the canvas object that you added as a widget is not a widget as far as Kivy is concerned. It doesn't subclass anything that Kivy recognizes as a widget. By its very nature, FigureCanvasAgg is completely independent of any GUI frameworks. You would need to have selected the appropriate backend for matplotlib to use prior to importing pyplot (I don't know which one Kivy is compatible with, GTK? QT? something else?).
By the way, chapter 5 of my book, "Interactive Applications Using Matplotlib" goes into detail explaining the ins and outs of GUI embedding with matplotlib. While I don't cover Kivy, I do a Rosetta Stone-like explanation covering GTK, Qt4, Wx, and Tk, and I explain the general concepts. Perhaps it might be useful?

http://www.amazon.com/Interactive-Applications-using-Matplotlib-Benjamin/dp/1783988843/
Cheers!
Ben Root

Best regards

David

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org<mailto:Matplotlib-users at python.org>
https://mail.python.org/mailman/listinfo/matplotlib-users

Click here<https://www.mailcontrol.com/sr/hMR6wrmFxrHGX2PQPOmvUg5!3elP1qzzXT9AmZTFOf3PtdjRxp3YcZ022EfPgnJAeE0+ZrrokCngE3KctAewCA==> to report this email as spam.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20151116/f783f1ce/attachment-0001.html>

David,

I should point out that Sandro's book is fairly old and that there is a new
one that replaces it: "Mastering Matplotlib". Note that neither book really
goes into any details about embedding figures into GUIs.

Ben

···

On Mon, Nov 16, 2015 at 12:37 PM, David Aldrich <David.Aldrich at emea.nec.com> wrote:

Hi Benjamin

Thanks for your reply. I have looked at your book on Amazon but thought I
should go for Sandro Tosi?s book initially, just to learn the basics of
Matplotlib. Perhaps I can get yours later J

Best regards

David

*From:* Benjamin Root [mailto:ben.v.root at gmail.com]
*Sent:* 16 November 2015 17:23
*To:* David Aldrich <David.Aldrich at EMEA.NEC.COM>
*Cc:* matplotlib-users at python.org
*Subject:* Re: [Matplotlib-users] Beginner questions about OO interface

Hello David,

On Mon, Nov 16, 2015 at 5:55 AM, David Aldrich <David.Aldrich at emea.nec.com> > wrote:

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?

Indeed, plt is the state machine interface, and it isn't exactly the same
thing to say "plt.show()" and to show a particular figure. You can call
`self.fig.show()`, though.

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?

That would work, but it is very inefficient. Most matplotlib artist
objects have some sort of "set_data()" or "set_offsets()" method that would
let you update the data contained in the artist. See the following
animation example:
http://matplotlib.org/examples/animation/animate_decay.html

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?

tight_layout() isn't the issue here (well, directly). The issue is that
the canvas object that you added as a widget is not a widget as far as Kivy
is concerned. It doesn't subclass anything that Kivy recognizes as a
widget. By its very nature, FigureCanvasAgg is completely independent of
any GUI frameworks. You would need to have selected the appropriate backend
for matplotlib to use prior to importing pyplot (I don't know which one
Kivy is compatible with, GTK? QT? something else?).

By the way, chapter 5 of my book, "Interactive Applications Using
Matplotlib" goes into detail explaining the ins and outs of GUI embedding
with matplotlib. While I don't cover Kivy, I do a Rosetta Stone-like
explanation covering GTK, Qt4, Wx, and Tk, and I explain the general
concepts. Perhaps it might be useful?

http://www.amazon.com/Interactive-Applications-using-Matplotlib-Benjamin/dp/1783988843/

Cheers!

Ben Root

Best regards

David

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Click here
<https://www.mailcontrol.com/sr/hMR6wrmFxrHGX2PQPOmvUg5!3elP1qzzXT9AmZTFOf3PtdjRxp3YcZ022EfPgnJAeE0+ZrrokCngE3KctAewCA==>
to report this email as spam.

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

Hi Ben

Yes, I take your point. Sometimes it?s hard to choose the right book from an online seller. I have "Mastering Matplotlib" but I think that assumes intermediate knowledge of Matplotlib, and I?m not there yet. Anyway, thanks for the advice.

David

···

From: Benjamin Root [mailto:ben.v.root@gmail.com]
Sent: 16 November 2015 17:42
To: David Aldrich <David.Aldrich at EMEA.NEC.COM>
Cc: matplotlib-users at python.org
Subject: Re: [Matplotlib-users] Beginner questions about OO interface

David,

I should point out that Sandro's book is fairly old and that there is a new one that replaces it: "Mastering Matplotlib". Note that neither book really goes into any details about embedding figures into GUIs.
Ben

On Mon, Nov 16, 2015 at 12:37 PM, David Aldrich <David.Aldrich at emea.nec.com<mailto:David.Aldrich at emea.nec.com>> wrote:
Hi Benjamin

Thanks for your reply. I have looked at your book on Amazon but thought I should go for Sandro Tosi?s book initially, just to learn the basics of Matplotlib. Perhaps I can get yours later ?

Best regards

David

From: Benjamin Root [mailto:ben.v.root at gmail.com<mailto:ben.v.root@gmail.com>]
Sent: 16 November 2015 17:23
To: David Aldrich <David.Aldrich at EMEA.NEC.COM<mailto:David.Aldrich at EMEA.NEC.COM>>
Cc: matplotlib-users at python.org<mailto:matplotlib-users at python.org>
Subject: Re: [Matplotlib-users] Beginner questions about OO interface

Hello David,

On Mon, Nov 16, 2015 at 5:55 AM, David Aldrich <David.Aldrich at emea.nec.com<mailto:David.Aldrich at emea.nec.com>> wrote:
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?

Indeed, plt is the state machine interface, and it isn't exactly the same thing to say "plt.show()" and to show a particular figure. You can call `self.fig.show()`, though.

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?

That would work, but it is very inefficient. Most matplotlib artist objects have some sort of "set_data()" or "set_offsets()" method that would let you update the data contained in the artist. See the following animation example: http://matplotlib.org/examples/animation/animate_decay.html

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?

tight_layout() isn't the issue here (well, directly). The issue is that the canvas object that you added as a widget is not a widget as far as Kivy is concerned. It doesn't subclass anything that Kivy recognizes as a widget. By its very nature, FigureCanvasAgg is completely independent of any GUI frameworks. You would need to have selected the appropriate backend for matplotlib to use prior to importing pyplot (I don't know which one Kivy is compatible with, GTK? QT? something else?).
By the way, chapter 5 of my book, "Interactive Applications Using Matplotlib" goes into detail explaining the ins and outs of GUI embedding with matplotlib. While I don't cover Kivy, I do a Rosetta Stone-like explanation covering GTK, Qt4, Wx, and Tk, and I explain the general concepts. Perhaps it might be useful?

http://www.amazon.com/Interactive-Applications-using-Matplotlib-Benjamin/dp/1783988843/
Cheers!
Ben Root

Best regards

David

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org<mailto:Matplotlib-users at python.org>
https://mail.python.org/mailman/listinfo/matplotlib-users

Click here<https://www.mailcontrol.com/sr/hMR6wrmFxrHGX2PQPOmvUg5!3elP1qzzXT9AmZTFOf3PtdjRxp3YcZ022EfPgnJAeE0+ZrrokCngE3KctAewCA==> to report this email as spam.

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