matplotlib on a server or in Docker: '_tkinter.TclError: no display name and no $DISPLAY'
@devsingh05▲ 10 net votes
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.