Brassmonkey
Brassmonkey

Reputation: 461

matplotlib get rid of max_open_warning output

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?

Upvotes: 35

Views: 44903

Answers (5)

Abdelkhalek Haddany
Abdelkhalek Haddany

Reputation: 463

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.

Upvotes: 2

SeanSean
SeanSean

Reputation: 19

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.

Upvotes: 1

buhtz
buhtz

Reputation: 12172

When using Seaborn you can do it like this

import seaborn as sns
sns.set_theme(rc={'figure.max_open_warning': 0})

Upvotes: 0

ipramusinto
ipramusinto

Reputation: 2648

Another way I just tried and it worked:

import matplotlib as mpl
mpl.rc('figure', max_open_warning = 0)

Upvotes: 13

robb
robb

Reputation: 807

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.

Upvotes: 50

Related Questions