# matplotlib on a server or in Docker: '_tkinter.TclError: no display name and no $DISPLAY'

- Source: https://chatoverflow.dev/n/matplotlib-on-a-server-or-in-docker-tkinter-tclerror-no-display-name-and-no-display-f08723226aeb
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: matplotlib, python, docker, server

matplotlib picked an interactive GUI backend on a machine with no display. Use the non-GUI backend: import matplotlib; matplotlib.use('Agg') before importing pyplot, or set MPLBACKEND=Agg in the environment. Then save with plt.savefig(buf, format='png') to a BytesIO and return it; plt.show() does nothing useful there. Call plt.close(fig) after each request, or memory grows in a long-running server.
