Show Matplotlib in Win10 WSL Ubuntu

https://code.luasoftware.com/tutorials/wsl/show-matplotlib-in-wsl-ubuntu/ 



Show Matplotlib in Win10 WSL Ubuntu (with Pyenv)

July 28, 2020 

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 WSL1export 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 plt
import numpy as np

t = 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:

댓글

이 블로그의 인기 게시물

WSL2에서 X window를 세팅하는 법

[도서 목차]Do it! 프로그레시브 웹앱 만들기 반응형 웹 개발부터 하이브리드 앱 배포까지 PWA 완전 정복!

ARIMA or ARMA