Plot $y=x^2-2x$ with gnuplot and matplotlib on WSL and VcXsrv X Server


Important note: Do not translate this page into Japanese with Google translation; otherwise there will be many execution errors.

Install WSL and X Server

Install WSL

If the Windows Subsystem for Linux (WSL) is not yet installed on your Windows system, you can execute the following command on the CMD command prompt as Administrator:

wsl --install

As a default, Ubuntu will be installed. After inputting a username and password on a Ubuntu terminal, you may update and upgrade Ubuntu packages:

sudo apt update
sudo apt upgrade
sudo apt autoremove

Then you can create a directory and a Python file

mkdir gnuplot
cd gnuplot

And copy the following contents to

# python3

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-0.5, 2.5, 301)
plt.plot(x, x*x - 2*x)

You may want to execute it as below immediately but the execution failed.


Traceback (most recent call last):
  File "", line 3, in 
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

You must install matplotlib first:

pip3 install matplotlib

Command 'pip3' not found, but can be installed with:

sudo apt install python3-pip

Install pip3:

sudo apt install python3-pip

Now, install matplotlib:

pip3 install matplotlib in requires an X window to show the figure, therefore we must install and start an X server.

Install X Server

If Cygwin/X is already installed on your PC, then start X on a Cygwin terminal:

xwin -multiwindow -listen tcp 2>/null &

Otherwise, you may install free VcXsrv, download it from, and install it. Then start Xlaunch and check "Disable access control", allowing anyone full access.

For using X server (Cygwin/X or VcXsrv), you must set $DISPLAY as below (WSL2 does not support localhost or

export DISPLAY=$(route.exe print | grep | head -1 | awk '{print $4}'):0.0

Execute as below, but the figure may not be shown:

python3 UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

To solve this problem, you may install python3-tk:

sudo apt install python3-tk

Then you can show the plot graph as below (Figure 1):




You may use emacs to edit the file:

emacs &

You can use gnome-terminal for easy copy and paste with your mouse:


To open windows explorer from the current working directory of the WSL shell:

explorer.exe .

You can access the windows directories and files:

ls -l /mnt/c

To use full LaTeX, install full TeXLive:

sudo apt install texlive-full
0 upgraded, 346 newly installed, 0 to remove and 0 not upgraded.
Need to get 3011 MB of archives.
After this operation, 5692 MB of additional disk space will be used.

To check disk space the /usr/share/texlive uses:

du -h /usr/share/texlive/
2.6G	/usr/share/texlive/

To find the location of a file, execute "locate filename". For example:

locate beamer.cls

Below are the settings added to ~/.profile in my case.

#export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0.0
export DISPLAY=$(route.exe print | grep | head -1 | awk '{print $4}'):0.0
export NO_AT_BRIDGE=1
export XDG_RUNTIME_DIR=/tmp/runtime-$(whoami)

You may use high DPI fonts by checking "High DPI scaling override" through the compatibility of the properties of vcxsrv.exe and xlaunch.exe in C:\Program Files\VcXsrv. Note: You can add a compatibility tab to the properties page if there is no compatibility tab originally.



Problem: WSL2 can no longer use X and network after using VPN (Cisco AnyConnect Secure Mobility Client on Windows).

Solved: Download and install anyconnect from Windows Store. See here.

BTW, the Cygwin X terminal works well with either VPN. We can execute "ssh -Y ..." on the Cygwin X terminal.

Gnuplot Templates

Refer to Cygwin version.