RuntimeError: Cannot open figure
7 Dec 2025
1 min read
RuntimeError: Cannot open figure
$ python -c "import matplotlib.pyplot as plt; plt.figure(); plt.show()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
RuntimeError: Cannot open figure
Why this happens
A GUI backend isn’t available in the current environment.
Fix
Switch to a compatible backend or save figures to files.
Wrong code
import matplotlib.pyplot as plt
plt.figure()
plt.show()
Fixed code
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.figure()
plt.savefig('figure.png')