matplotlib get rid of max_open_warning output
Asked Answered
S

5

34

I wrote a script that calls functions from QIIME to build a bunch of plots among other things. Everything runs fine to completion, but matplotlib always throws the following feedback for every plot it creates (super annoying):

/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py:412: 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_num_figures). max_open_warning, RuntimeWarning)

I found this page which seems to explain how to fix this problem , but after I follow directions, nothing changes:

import matplotlib as mpl
mpl.rcParams['figure.max_open_warning'] = 0

I went into the file after calling matplotlib directly from python to see which rcparams file I should be investigating and manually changed the 20 to 0. Still no change. In case the documentation was incorrect, I also changed it to 1000, and still am getting the same warning messages.

I understand that this could be a problem for people running on computers with limited power, but that isn't a problem in my case. How can I make this feedback go away permanently?

Sarcenet answered 15/12, 2014 at 2:45 Comment(2)
Try this one: #21884771Hegyera
There is just one opening quote missing before the dictionary key, otherwise this is correct!Folberth
C
48

Try setting it this way:

import matplotlib as plt
plt.rcParams.update({'figure.max_open_warning': 0})

Not sure exactly why this works, but it mirrors the way I have changed the font size in the past and seems to fix the warnings for me.

Cranston answered 20/12, 2015 at 4:6 Comment(0)
F
13

Another way I just tried and it worked:

import matplotlib as mpl
mpl.rc('figure', max_open_warning = 0)
Farlee answered 27/4, 2019 at 16:56 Comment(0)
P
2

In Matplotlib, figure.max_open_warning is a configuration parameter that determines the maximum number of figures that can be opened before a warning is issued. By default, the value of this parameter is 20. This means that if you open more than 20 figures in a single Matplotlib session, you will see a warning message. You can change the value of this parameter by using the matplotlib.rcParams function. For example:

import matplotlib.pyplot as plt
plt.rcParams['figure.max_open_warning'] = 50

This will set the value of figure.max_open_warning to 50, so that you will see a warning message if you open more than 50 figures in a single Matplotlib session.

Poirer answered 1/1, 2023 at 11:36 Comment(0)
C
1

Check out this article which basically says to plt.close(fig1) after you're done with fig1. This way you don't have too many figs floating around in memory.

Cato answered 16/6, 2021 at 2:29 Comment(0)
C
0

When using Seaborn you can do it like this

import seaborn as sns
sns.set_theme(rc={'figure.max_open_warning': 0})
Cavalierly answered 13/12, 2022 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.