[Matplotlib-users] mplcairo multipage pdf and too many figures open

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
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

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 ndbecker2@gmail.com 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

Matplotlib-users@python.org

https://mail.python.org/mailman/listinfo/matplotlib-users

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 <antony.lee@institutoptique.fr> 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 <ndbecker2@gmail.com> 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
Matplotlib-users@python.org
Matplotlib-users Info Page

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

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 ndbecker2@gmail.com 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 > > antony.lee@institutoptique.fr 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 ndbecker2@gmail.com 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

Matplotlib-users@python.org

https://mail.python.org/mailman/listinfo/matplotlib-users

Those who don’t understand recursion are doomed to repeat it


Matplotlib-users mailing list

Matplotlib-users@python.org

https://mail.python.org/mailman/listinfo/matplotlib-users

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 <mobiusklein@gmail.com> 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 <ndbecker2@gmail.com> 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 >> <antony.lee@institutoptique.fr> 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 <ndbecker2@gmail.com> 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
>> Matplotlib-users@python.org
>> Matplotlib-users Info Page

--
Those who don't understand recursion are doomed to repeat it
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
Matplotlib-users Info Page

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