python TclError: no display name and no $DISPLAY environment variable code example
Example: _tkinter.tclerror: no display name and no $display environment variable
Matplotlib chooses Xwindows backend by default. You need to set matplotlib to not use the Xwindows backend.
Add this code to the start of your script (before importing pyplot) and try again:
import matplotlib
matplotlib.use('Agg')
Or add to .config/matplotlib/matplotlibrc line backend: Agg to use non-interactive backend.
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc
Or when connect to server use ssh -X remoteMachine command to use Xwindows.
Also you may try to export display: export DISPLAY=mymachine.com:0.0.
For more info: https://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server