emonigma
emonigma

Reputation: 4440

Close a silent figure opened with pandas

I plot multiple figures in pandas and save them to a file with:

import pandas as pd

for i in range(30):  # loop example
    ax = df.plot(x=date_name, y=metric, legend=False, figsize=(4.5, 3))
    ax.set_title(title, fontdict={'fontsize': 14, 'fontweight': 'medium'})
    ax.set(xlabel=None)

Following how to close pandas dataframe plot, I have tried:

import matplotlib.pyplot as plt

plt.close(ax.get_figure())
plt.close(ax.figure)

and still get the error:

/usr/local/lib/python3.7/site-packages/matplotlib/pyplot.py:514: 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`).

How can I close a silent figure?

Upvotes: 1

Views: 297

Answers (1)

Sandell0
Sandell0

Reputation: 112

  1. The code that you used was OK and should have solved this problem.
   import matplotlib.pyplot as plt
   for i in range(30):  # loop example
      ...
      plt.close(ax.get_figure())
  1. In case you still face issues after this, try to comment out lines that plot those figures and see if the error remains. If yes, that means that you have some other place that produces those redundant plots.

  2. If nothing helped you can try to use plt.close('all') that will close all figure windows.

Upvotes: 1

Related Questions