If you are using Pyenv, you must build python with tk support. Else, you need to run sudo apt-get install python3-tk
or sudo apt-get install python-tk
.
If you are WSL2
, you need to run
export DISPLAY=`grep -oP "(?<=nameserver ).+" /etc/resolv.conf`:0.0
(getting the IP of Windows Host/X Server from /etc/resolv.conf
). If you are on WSL1
, export DISPLAY=localhost:0.0
will do. You might want to add this to ~/.bashrc
.
You need to install X Servers for Windows, using either VcXsrv or Xming. In only tested with Xming. When prompted for the firewall access, allow both private and public network (If you miss that, you can set it at Windows Security -> Firewall & network protection -> Allow an app through firewall
. Launch Xming (you will see the app icon at bottom-right corner next to the clock, expand to see).
Run the code
import matplotlib.pyplot as pltimport numpy as npt = np.arange(0.0, 2.0, 0.01)s = 1 + np.sin(2*np.pi*t)plt.plot(t, s)plt.title('About as simple as it gets, folks')plt.show()
You might bump into error _tkinter.TclError: couldn't connect to display "172.29.0.1:0.0"
.
Check you Xming log (right click bottom-right Xming app icon and click View log
and you should see
AUDIT: Tue Jul 28 18:31:11 2020: 20788 C:\Program Files (x86)\Xming\Xming.exe: client 4 rejected from IP 172.29.10.201
Edit C:\Program Files (x86)\Xming\X0.hosts
(open with Administrator priviledge to enable edit/save). Add the rejected IP.
localhost
172.29.10.201
NOTE: Note that WSL Ubuntu IP is not static
Exit Xming server and start again.
References: