Python Plotting with elpy on wsl2

Having started and stopped many times I’ve decided that I am finally going learn to use python. I’m a devoted emacs user and I love my Surface Pro 4, but I also prefer Linux to Windows, so my setup is a little unusual. Since the Surface isn’t fully functional under Linux I run Ubuntu through wsl2 with XWinrc as my Xwindows server to get the best of both worlds.

I’m currently using elpy as my development environment within emacs, but for some reason plots weren’t working correctly. Printing a plot would show a new window, but the window would remain black and I couldn’t close it. Plots worked fine if I ran python directly in the console, so it had something to do with the interplay between python, emacs, and elpy. After some tinkering, I came up with the following solution.

  1. Install tkinter with the command:
    sudo apt-get install python-tk python3-tk tk-dev
  2. Create the file ~/.config/matplotlib/matplotlibrc with the single line
    backend: TkAgg
    or add this line if the file already exists.

This changes the plotting backend for python and now everything works perfectly.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.