[Matplotlib-users] Beginner questions about OO interface

Thomas Caswell tcaswell at gmail.com
Mon Nov 16 12:33:19 EST 2015


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>


More information about the Matplotlib-users mailing list