Preserving figure over closes and reopenings of the window

I'm using matplotlib for drawing lines, points and circles basically. I have 2 problems i can't solve by myself and it's hard to solve them not digging into source i think.
First problem: I got an instance of figure class and i want to preserve it during closing and reopening the output window. I'm curious what is happening after clicking on close button on the main widow and is there any way to reopen previously saved figure, and if yes how to save that figure for future reopenings.
Second problem: I want to achieve 3 things simultaneously,
a) have the axes extended on the whole figure area
b) unit distance on y axis has to be the same as unit distance on x axis
c) all above has to work well with autoscale(), i want to have a lot of artists on the axes
The best thing i have is setting aspect as equal and using autoscale, but it fix axes to be same lenght and i don't want axes to be same length i want the unit distance to be the same

Thanks for any help,
Maciek

Maciej,

When you close a figure, just about everything gets destroyed (this is why you should always savefig() prior to doing a show()). There is a mechanism for “reloading” a plot using python pickles:

http://stackoverflow.com/questions/7290370/store-and-reload-matplotlib-pyplot-object

As for setting the aspect ratio to ‘equal’, there is a second optional argument that indicates which thing should get adjusted to achieve an equal aspect. By default, it is ‘datalim’, I think, which adjusts the limits of the axes. I think if you set it to ‘box’, it will do what you want (you might need ‘box-forced’ if you are sharing axes). Fiddle around with these settings to get what you want.

http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_aspect

To have the axes extended on the whole figure area, there are multiple ways to do that. In particular, you can set the margins for the figure:

http://matplotlib.org/api/pyplot_api.html?highlight=subplots_adjust#matplotlib.pyplot.subplots_adjust

All of this would work just fine with autoscale.

I hope that helps!

Ben Root

···

On Mon, Aug 25, 2014 at 7:00 AM, Maciej Kurnicki <kurnicki.m@…287…> wrote:

I’m using matplotlib for drawing lines, points and circles basically. I

have 2 problems i can’t solve by myself and it’s hard to solve them not

digging into source i think.

First problem: I got an instance of figure class and i want to preserve

it during closing and reopening the output window. I’m curious what is

happening after clicking on close button on the main widow and is there

any way to reopen previously saved figure, and if yes how to save that

figure for future reopenings.

Second problem: I want to achieve 3 things simultaneously,

a) have the axes extended on the whole figure area

b) unit distance on y axis has to be the same as unit distance on x axis

c) all above has to work well with autoscale(), i want to have a lot of

artists on the axes

The best thing i have is setting aspect as equal and using autoscale,

but it fix axes to be same lenght and i don’t want axes to be same

length i want the unit distance to be the same

Thanks for any help,

Maciek


Slashdot TV.

Video for Nerds. Stuff that matters.

http://tv.slashdot.org/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

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

Ben,

The adjusting and aspect solution works just perfect, thanks for your fast answer. Although i still want to ask for another solution for reloading a plot, because i don't like the idea of serialization and writing to disk. From what i've found even if i have a variables handling figure and axis when i quit the window some part of it probably form the window framework are deleted anyway. In this case i think i can redraw all the artists on the newly created figure somehow but is there a relatively simple way to just set up new window and attach the old figure there?

Maciej

W dniu 25.08.2014 o 16:00, Benjamin Root pisze:

···

Maciej,

When you close a figure, just about everything gets destroyed (this is why you should always savefig() prior to doing a show()). There is a mechanism for "reloading" a plot using python pickles:
http://stackoverflow.com/questions/7290370/store-and-reload-matplotlib-pyplot-object

As for setting the aspect ratio to 'equal', there is a second optional argument that indicates which thing should get adjusted to achieve an equal aspect. By default, it is 'datalim', I think, which adjusts the limits of the axes. I think if you set it to 'box', it will do what you want (you might need 'box-forced' if you are sharing axes). Fiddle around with these settings to get what you want.
http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_aspect

To have the axes extended on the whole figure area, there are multiple ways to do that. In particular, you can set the margins for the figure:
http://matplotlib.org/api/pyplot_api.html?highlight=subplots_adjust#matplotlib.pyplot.subplots_adjust

All of this would work just fine with autoscale.

I hope that helps!
Ben Root

On Mon, Aug 25, 2014 at 7:00 AM, Maciej Kurnicki <kurnicki.m@…287… > <mailto:kurnicki.m@…287…>> wrote:

    I'm using matplotlib for drawing lines, points and circles
    basically. I
    have 2 problems i can't solve by myself and it's hard to solve
    them not
    digging into source i think.
    First problem: I got an instance of figure class and i want to
    preserve
    it during closing and reopening the output window. I'm curious what is
    happening after clicking on close button on the main widow and is
    there
    any way to reopen previously saved figure, and if yes how to save that
    figure for future reopenings.
    Second problem: I want to achieve 3 things simultaneously,
    a) have the axes extended on the whole figure area
    b) unit distance on y axis has to be the same as unit distance on
    x axis
    c) all above has to work well with autoscale(), i want to have a
    lot of
    artists on the axes
    The best thing i have is setting aspect as equal and using autoscale,
    but it fix axes to be same lenght and i don't want axes to be same
    length i want the unit distance to be the same

    Thanks for any help,
    Maciek

    ------------------------------------------------------------------------------
    Slashdot TV.
    Video for Nerds. Stuff that matters.
    http://tv.slashdot.org/
    _______________________________________________
    Matplotlib-users mailing list
    Matplotlib-users@lists.sourceforge.net
    <mailto:Matplotlib-users@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I am glad that solution worked for you. As for an alternative to pickling, no there is no current mechanism to do what you want easily. It is by design to start destroying everything upon close. We get enough complaints about “memory” leaks as it is. What you are asking for is essentially serialization, you just don’t want it written to disk. The good news is that you don’t have to write to disk. The pickle module accepts a file-like object, so you could dump to a cStringIO object, rewind that stream and then reload that data from the cStringIO object.

Now, maybe some work could be done to improve things, but as it stands right now, this is the only method I can think of to do what you want. Maybe one of the other devs has a better idea.

I hope that helps!

Ben

···

On Mon, Aug 25, 2014 at 11:50 AM, Maciej Kurnicki <kurnicki.m@…1003…7…> wrote:

Ben,

  The adjusting and aspect solution works just perfect, thanks for

your fast answer. Although i still want to ask for another
solution for reloading a plot, because i don’t like the idea of
serialization and writing to disk. From what i’ve found even if i
have a variables handling figure and axis when i quit the window
some part of it probably form the window framework are deleted
anyway. In this case i think i can redraw all the artists on the
newly created figure somehow but is there a relatively simple way
to just set up new window and attach the old figure there?

  Maciej



  W dniu 25.08.2014 o 16:00, Benjamin Root pisze:

Maciej,

              When you close a figure, just about everything gets

destroyed (this is why you should always savefig()
prior to doing a show()). There is a mechanism for
“reloading” a plot using python pickles:

              [http://stackoverflow.com/questions/7290370/store-and-reload-matplotlib-pyplot-object](http://stackoverflow.com/questions/7290370/store-and-reload-matplotlib-pyplot-object)
            As for setting the aspect ratio to 'equal', there is a

second optional argument that indicates which thing
should get adjusted to achieve an equal aspect. By
default, it is ‘datalim’, I think, which adjusts the
limits of the axes. I think if you set it to ‘box’, it
will do what you want (you might need ‘box-forced’ if
you are sharing axes). Fiddle around with these settings
to get what you want.

            [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_aspect](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_aspect)
          To have the axes extended on the whole figure area, there

are multiple ways to do that. In particular, you can set
the margins for the figure:

          [http://matplotlib.org/api/pyplot_api.html?highlight=subplots_adjust#matplotlib.pyplot.subplots_adjust](http://matplotlib.org/api/pyplot_api.html?highlight=subplots_adjust#matplotlib.pyplot.subplots_adjust)

All of this would work just fine with autoscale.

I hope that helps!

Ben Root

      On Mon, Aug 25, 2014 at 7:00 AM, Maciej

Kurnicki <kurnicki.m@…287…>
wrote:

        I'm using

matplotlib for drawing lines, points and circles basically.
I

        have 2 problems i can't solve by myself and it's hard to

solve them not

        digging into source i think.

        First problem: I got an instance of figure class and i want

to preserve

        it during closing and reopening the output window. I'm

curious what is

        happening after clicking on close button on the main widow

and is there

        any way to reopen previously saved figure, and if yes how to

save that

        figure for future reopenings.

        Second problem: I want to achieve 3 things simultaneously,

        a) have the axes extended on the whole figure area

        b) unit distance on y axis has to be the same as unit

distance on x axis

        c) all above has to work well with autoscale(), i want to

have a lot of

        artists on the axes

        The best thing i have is setting aspect as equal and using

autoscale,

        but it fix axes to be same lenght and i don't want axes to

be same

        length i want the unit distance to be the same



        Thanks for any help,

        Maciek

        Slashdot TV.

        Video for Nerds.  Stuff that matters.

        [http://tv.slashdot.org/](http://tv.slashdot.org/)

        _______________________________________________

        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)