mplcairo multipage pdf and too many figures open

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

mplcairo multipage pdf and too many figures open

Neal Becker
I've been using mplcairo multipage pdf.  The pattern I use is:

from mplcairo.multipage import MultiPage
cm = MultiPage(ap_args.output)
 
with cm as pdf:
  fig,ax = plt.subplots()
  do something
  pdf.savefig(fig)

  fig2,ax2 = plt.subplots()
  do something
  pdf.savefig(fig2)
...

after 20 figures:
RuntimeWarning: More than 20 figures have been opened. Figures created
through the pyplot interface (`matplotlib.pyplot.figure`) are retained until
explicitly closed and may consume too much memory. (To control this warning,
see the rcParam `figure.max_open_warning`).

What is the proper way to use mplcairo.multipage to avoid this issue?

Thanks,
Neal

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: mplcairo multipage pdf and too many figures open

Antony Lee-3
Hi,
You can probably either call plt.close(fig) after savefig to close the figure (likely best), or adjust rcParams["figure.max_open_warning"] to a large enough number (at the risk of running out of memory).
Antony

On Fri, Aug 16, 2019 at 12:44 AM Neal Becker <[hidden email]> wrote:
I've been using mplcairo multipage pdf.  The pattern I use is:

from mplcairo.multipage import MultiPage
cm = MultiPage(ap_args.output)

with cm as pdf:
  fig,ax = plt.subplots()
  do something
  pdf.savefig(fig)

  fig2,ax2 = plt.subplots()
  do something
  pdf.savefig(fig2)
...

after 20 figures:
RuntimeWarning: More than 20 figures have been opened. Figures created
through the pyplot interface (`matplotlib.pyplot.figure`) are retained until
explicitly closed and may consume too much memory. (To control this warning,
see the rcParam `figure.max_open_warning`).

What is the proper way to use mplcairo.multipage to avoid this issue?

Thanks,
Neal

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: mplcairo multipage pdf and too many figures open

Neal Becker
I recoded as:

    if pdf: pdf.savefig (fig) and plt.close(fig)

And I still get the same warning message.

On Fri, Aug 16, 2019 at 4:46 AM Antony Lee
<[hidden email]> wrote:

>
> Hi,
> You can probably either call plt.close(fig) after savefig to close the figure (likely best), or adjust rcParams["figure.max_open_warning"] to a large enough number (at the risk of running out of memory).
> Antony
>
> On Fri, Aug 16, 2019 at 12:44 AM Neal Becker <[hidden email]> wrote:
>>
>> I've been using mplcairo multipage pdf.  The pattern I use is:
>>
>> from mplcairo.multipage import MultiPage
>> cm = MultiPage(ap_args.output)
>>
>> with cm as pdf:
>>   fig,ax = plt.subplots()
>>   do something
>>   pdf.savefig(fig)
>>
>>   fig2,ax2 = plt.subplots()
>>   do something
>>   pdf.savefig(fig2)
>> ...
>>
>> after 20 figures:
>> RuntimeWarning: More than 20 figures have been opened. Figures created
>> through the pyplot interface (`matplotlib.pyplot.figure`) are retained until
>> explicitly closed and may consume too much memory. (To control this warning,
>> see the rcParam `figure.max_open_warning`).
>>
>> What is the proper way to use mplcairo.multipage to avoid this issue?
>>
>> Thanks,
>> Neal
>>
>> _______________________________________________
>> Matplotlib-users mailing list
>> [hidden email]
>> https://mail.python.org/mailman/listinfo/matplotlib-users



--
Those who don't understand recursion are doomed to repeat it
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: mplcairo multipage pdf and too many figures open

Joshua Klein
`pdf.savefig` returns None (it has no explicit return value), which is a False-y value. This causes the `and` expression to short-circuit and the right-hand-side expression is never evaluated. You'll need to call them separately.

On Fri, Aug 16, 2019 at 7:17 AM Neal Becker <[hidden email]> wrote:
I recoded as:

    if pdf: pdf.savefig (fig) and plt.close(fig)

And I still get the same warning message.

On Fri, Aug 16, 2019 at 4:46 AM Antony Lee
<[hidden email]> wrote:
>
> Hi,
> You can probably either call plt.close(fig) after savefig to close the figure (likely best), or adjust rcParams["figure.max_open_warning"] to a large enough number (at the risk of running out of memory).
> Antony
>
> On Fri, Aug 16, 2019 at 12:44 AM Neal Becker <[hidden email]> wrote:
>>
>> I've been using mplcairo multipage pdf.  The pattern I use is:
>>
>> from mplcairo.multipage import MultiPage
>> cm = MultiPage(ap_args.output)
>>
>> with cm as pdf:
>>   fig,ax = plt.subplots()
>>   do something
>>   pdf.savefig(fig)
>>
>>   fig2,ax2 = plt.subplots()
>>   do something
>>   pdf.savefig(fig2)
>> ...
>>
>> after 20 figures:
>> RuntimeWarning: More than 20 figures have been opened. Figures created
>> through the pyplot interface (`matplotlib.pyplot.figure`) are retained until
>> explicitly closed and may consume too much memory. (To control this warning,
>> see the rcParam `figure.max_open_warning`).
>>
>> What is the proper way to use mplcairo.multipage to avoid this issue?
>>
>> Thanks,
>> Neal
>>
>> _______________________________________________
>> Matplotlib-users mailing list
>> [hidden email]
>> https://mail.python.org/mailman/listinfo/matplotlib-users



--
Those who don't understand recursion are doomed to repeat it
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: mplcairo multipage pdf and too many figures open

Neal Becker
Thanks! I rarely use this single line form of 'if' and screwed it up.

On Fri, Aug 16, 2019 at 7:25 AM Joshua Klein <[hidden email]> wrote:

>
> `pdf.savefig` returns None (it has no explicit return value), which is a False-y value. This causes the `and` expression to short-circuit and the right-hand-side expression is never evaluated. You'll need to call them separately.
>
> On Fri, Aug 16, 2019 at 7:17 AM Neal Becker <[hidden email]> wrote:
>>
>> I recoded as:
>>
>>     if pdf: pdf.savefig (fig) and plt.close(fig)
>>
>> And I still get the same warning message.
>>
>> On Fri, Aug 16, 2019 at 4:46 AM Antony Lee
>> <[hidden email]> wrote:
>> >
>> > Hi,
>> > You can probably either call plt.close(fig) after savefig to close the figure (likely best), or adjust rcParams["figure.max_open_warning"] to a large enough number (at the risk of running out of memory).
>> > Antony
>> >
>> > On Fri, Aug 16, 2019 at 12:44 AM Neal Becker <[hidden email]> wrote:
>> >>
>> >> I've been using mplcairo multipage pdf.  The pattern I use is:
>> >>
>> >> from mplcairo.multipage import MultiPage
>> >> cm = MultiPage(ap_args.output)
>> >>
>> >> with cm as pdf:
>> >>   fig,ax = plt.subplots()
>> >>   do something
>> >>   pdf.savefig(fig)
>> >>
>> >>   fig2,ax2 = plt.subplots()
>> >>   do something
>> >>   pdf.savefig(fig2)
>> >> ...
>> >>
>> >> after 20 figures:
>> >> RuntimeWarning: More than 20 figures have been opened. Figures created
>> >> through the pyplot interface (`matplotlib.pyplot.figure`) are retained until
>> >> explicitly closed and may consume too much memory. (To control this warning,
>> >> see the rcParam `figure.max_open_warning`).
>> >>
>> >> What is the proper way to use mplcairo.multipage to avoid this issue?
>> >>
>> >> Thanks,
>> >> Neal
>> >>
>> >> _______________________________________________
>> >> Matplotlib-users mailing list
>> >> [hidden email]
>> >> https://mail.python.org/mailman/listinfo/matplotlib-users
>>
>>
>>
>> --
>> Those who don't understand recursion are doomed to repeat it
>> _______________________________________________
>> Matplotlib-users mailing list
>> [hidden email]
>> https://mail.python.org/mailman/listinfo/matplotlib-users



--
Those who don't understand recursion are doomed to repeat it
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users