creating an object oriented matplotlib figure

I want to create a matplotlib figure as part of a program I am writing and therefore would like to create the figure in a fully object oriented way, ie not in the pyplot state-full way. I understand how to work with a figure object to create axes objects and then fill the axes objects with primitive artists but I can't figure out what type of object to put the figure object in to get it displayed. From digging around in the source it looks like the show() method comes from the backend, but when I looked in 'matplotlib/backends/backend_agg' I saw no such object. What am I missing? I guess I could put the figure in a Qt window, but I like the functionality provided by the default matplotlib window.

Thanks for the help

Hi Kelson,

···

2013/1/16 Kelson Zawack <kbz6@…163…>

I want to create a matplotlib figure as part of a program I am writing

and therefore would like to create the figure in a fully object oriented

way, ie not in the pyplot state-full way. I understand how to work with

a figure object to create axes objects and then fill the axes objects

with primitive artists but I can’t figure out what type of object to put

the figure object in to get it displayed.
using qt4agg for me this works (version 1.2.0)

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot([1,2])

fig.show()

From digging around in the

source it looks like the show() method comes from the backend, but when

I looked in ‘matplotlib/backends/backend_agg’ I saw no such object.

What am I missing? I guess I could put the figure in a Qt window, but I

like the functionality provided by the default matplotlib window.

Thanks for the help

does this help?

Francesco


Master Java SE, Java EE, Eclipse, Spring, Hibernate, JavaScript, jQuery

and much more. Keep your Java skills current with LearnJavaNow -

200+ hours of step-by-step video tutorials by Java experts.

SALE $49.99 this month only – learn more at:

http://p.sf.net/sfu/learnmore_122612


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

“agg” is not an interactive backend and is useful only when saving figures (http://matplotlib.1069221.n5.nabble.com/Backend-Agg-show-plot-td13162.html). Thats why there is no show().

e.g. qt4agg gets show from qt4

Ok, I understand about agg, but I am still a bit confused. First
when I run the suggested code using whatever the default backend is
the figure is only displayed for a second and then it goes away and
the program ends. I guess what I am really interested in is what
plt.figure() does. It seems to be creating a figure manager which
has a canvas and a figure in it, but which one of these is
responsible for the showing/saving to file?

Thanks for your help
···

On 1/16/13 5:02 AM, Francesco Montesano
wrote:

Hi Kelson,

2013/1/16 Kelson Zawack <kbz6@…163…>

          I

want to create a matplotlib figure as part of a program I
am writing

          and therefore would like to create the figure in a fully

object oriented

          way, ie not in the pyplot state-full way.  I understand

how to work with

          a figure object to create axes objects and then fill the

axes objects

          with primitive artists but I can't figure out what type of

object to put

          the figure object in to get it displayed.

using qt4agg for me this works (version 1.2.0)

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot([1,2])

fig.show()

          From

digging around in the

          source it looks like the show() method comes from the

backend, but when

          I looked in 'matplotlib/backends/backend_agg' I saw no

such object.

          What am I missing?  I guess I could put the figure in a Qt

window, but I

          like the functionality provided by the default matplotlib

window.

          Thanks

for the help

            "agg" is not an interactive

backend and is useful only when saving figures (http://matplotlib.1069221.n5.nabble.com/Backend-Agg-show-plot-td13162.html ).
Thats why there is no show().

            e.g. qt4agg gets show

from qt4

does this help?

Francesco


          Master Java SE, Java EE, Eclipse, Spring, Hibernate,

JavaScript, jQuery

          and much more. Keep your Java skills current with

LearnJavaNow -

          200+ hours of step-by-step video tutorials by Java

experts.

          SALE $49.99 this month only -- learn more at:

          [http://p.sf.net/sfu/learnmore_122612](http://p.sf.net/sfu/learnmore_122612)

          _______________________________________________

          Matplotlib-users mailing list

          Matplotlib-users@lists.sourceforge.net

          [https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)

2013/1/16 Kelson Zawack <kbz6@...163...>:

I want to create a matplotlib figure as part of a program I am writing
and therefore would like to create the figure in a fully object oriented
way, ie not in the pyplot state-full way. I understand how to work with
a figure object to create axes objects and then fill the axes objects
with primitive artists but I can't figure out what type of object to put
the figure object in to get it displayed. From digging around in the
source it looks like the show() method comes from the backend, but when
I looked in 'matplotlib/backends/backend_agg' I saw no such object.
What am I missing? I guess I could put the figure in a Qt window, but I
like the functionality provided by the default matplotlib window.

Thanks for the help

Hi,
if you need an embedded figure in some gui app (supported with
matplotlib backends),
you may check some demos on this topic:
http://matplotlib.org/examples/user_interfaces/index.html
embedding_in_ ... (gtk, tk, qt, wx)
You can also reuse the gui elements such as toolbar etc.
I have no experience with qt, but apparently, there is an example for this:
http://matplotlib.org/examples/user_interfaces/embedding_in_qt4_wtoolbar.html
Or is it some more specific problem, you are encountering?

hth,
   vbr

Perhaps you will find it useful to look at the TSPlot class at
http://econpy.googlecode.com/svn-history/r175/trunk/abm/gridworld/gridworld.py

Alan Isaac

···

On 1/16/2013 4:39 AM, Kelson Zawack wrote:

I want to create a matplotlib figure as part of a program I am writing
and therefore would like to create the figure in a fully object oriented
way, ie not in the pyplot state-full way.

If the plot only appears for a moment, then that would be a bug. Could you double-check which version of matplotlib you are running?

Ben Root

···

On Wed, Jan 16, 2013 at 5:50 AM, Kelson Zawack <kbz6@…163…> wrote:

Ok, I understand about agg, but I am still a bit confused.  First

when I run the suggested code using whatever the default backend is
the figure is only displayed for a second and then it goes away and
the program ends. I guess what I am really interested in is what
plt.figure() does. It seems to be creating a figure manager which
has a canvas and a figure in it, but which one of these is
responsible for the showing/saving to file?

Thanks for your help

I am running 1.2.0

···

On 1/16/13 10:23 AM, Benjamin Root
wrote:

    On Wed, Jan 16, 2013 at 5:50 AM, Kelson

Zawack <kbz6@…163…>
wrote:

        Ok, I understand about

agg, but I am still a bit confused. First when I run the
suggested code using whatever the default backend is the
figure is only displayed for a second and then it goes away
and the program ends. I guess what I am really interested
in is what plt.figure() does. It seems to be creating a
figure manager which has a canvas and a figure in it, but
which one of these is responsible for the showing/saving to
file?

        Thanks for your help
      If the plot only appears for a moment, then that would be a

bug. Could you double-check which version of matplotlib you
are running?

      Ben Root

So I looked into it and the reason calling the show method for a
Figure/FigureManager causes the figure to be visible only for an
instant is that, at least in the case for tkagg, there is no call to
a function that blocks until the figure window is closed and as a
result the figure is displayed then the call to show ends, the
figure closes, and the program moves on. This is in contrast to the
behavior of the callable object invoked by pyplot.show() which calls
a mainloop method to block. Since I am not super familiar with the
internals of Matplotlib I am not sure the best way to fix this, but
one option, in the case of tkagg, is to add a call to tk.mainloop()
to the end of FigureManagerTkAgg.show() as is done in tkagg’s Show
object.

Just my 2 cents
···

On 1/16/13 10:23 AM, Benjamin Root
wrote:

    On Wed, Jan 16, 2013 at 5:50 AM, Kelson

Zawack <kbz6@…163…>
wrote:

        Ok, I understand about

agg, but I am still a bit confused. First when I run the
suggested code using whatever the default backend is the
figure is only displayed for a second and then it goes away
and the program ends. I guess what I am really interested
in is what plt.figure() does. It seems to be creating a
figure manager which has a canvas and a figure in it, but
which one of these is responsible for the showing/saving to
file?

        Thanks for your help
      If the plot only appears for a moment, then that would be a

bug. Could you double-check which version of matplotlib you
are running?

      Ben Root