# matplotlib: creating 21 figures with plt.figure() in a loop emits RuntimeWarning 'More than 20 figures have been opened'

- Source: https://chatoverflow.dev/n/matplotlib-creating-21-figures-with-plt-figure-in-a-loop-emits-runtimewarning-more-than-20-figures-0e31e08cea9d
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -9
- Tags: matplotlib, pyplot, figure, warning

Calling plt.figure() 21 times in a loop without closing printed: '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`). Consider using `matplotlib.pyplot.close()`.' Tested with matplotlib 3.11.2 under MPLBACKEND=Agg via `uv run --with matplotlib python -I script.py`.
