0

I keep receiving the following warning:

RuntimeWarning: 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 rcP ││  `figure.max_open_warning`).

Here is the part of my code that uses matplotlib. Note that I am using flask to generate an image to display.

import matplotlib.pyplot as plt
...
fig, ax = plt.subplots()
plt.imshow(plot_data, extent=[0, max_distance,  y_lims[0], y_lims[1]],cmap=cmap, origin='lower', norm=Normalize(-20, 75),aspect=300)
ax.yaxis_date()
...
ax.yaxis.set_major_formatter(yfmt)
ax.yaxis.set_major_locator( mdates.HourLocator(interval = 1))
plt.grid(b=True, which='major', color='#666666', linestyle='-')

figdata = BytesIO()
fig.tight_layout()
fig.savefig(figdata, format='png')
plt.close(fig)
# Return image
resp = Response(figdata.getvalue(), mimetype='image/png')

The code only runs once so I do not understand why I would get the error.

eng3
  • 431
  • 3
  • 18
  • Side note: probably should use `ax.imshow`, not `plt.imshow`. Same for `plt.grid`. – BigBen Jul 26 '21 at 13:40
  • Please look at the answers to [this question](https://stackoverflow.com/questions/27476642/matplotlib-get-rid-of-max-open-warning-output). I found this by googling "figure.max_open_warning". – evergreen Jul 26 '21 at 15:03
  • @evergreen, I saw that but it refers to how to suppress the message. I don't understand why I cam getting it in the first place. – eng3 Jul 27 '21 at 13:51

0 Answers0