Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Matplotlib backend when using Windows Subsystem for Linux

My Matplotlib backend keeps reverting to TkAgg. This is a problem because in the Windows Subsystem for Linux (WSL), you can't do GUI stuff, and so I get the error

TclError: no display name and no $DISPLAY environment variable

I've tried adding a matplotlibrc file to /home/<user>/.config/matplotlib (in the Windows filesystem, this is C:\Users\<user>\AppData\Local\lxss\home\<user>\.config\matplotlib).

My matplotlibrc looks like this

backend : Agg

However, if I do this

$ cd /home/<user>/.config/matplotlib
$ ls -A

nothing shows up.

When I try

 $ python
 >>> import matplotlib
 >>> matplotlib.get_backend()
 'TkAgg'

so clearly it's not setting the backend to Agg. Why not?

Update:

I've figured out that putting backend : Agg in C:\Users\<user>\AppData\Local\lxss\home\<user>\.config\matplotlib\matplotlibrc changes the backend in Windows Python only, leaving Linux Python as TkAgg. Which is odd, because Windows Python should only be using C:\Users\<user>\AppData\Local\Enthought\Canopy\User\Lib\site-packages\matplotlib\mpl-data\matplotlibrc, right?

like image 969
binaryfunt Avatar asked Oct 18 '22 22:10

binaryfunt


1 Answers

I figured it out. I had created the matplotlibrc file using Windows - this is why it didn't show up when I tried $ ls -A in bash. So I did this instead (after deleting the Windows-created file):

$ cd /home/<user>/.config/matplotlib
$ touch matplotlibrc
$ echo "backend : Agg" > matplotlibrc

(touch matplotlibrc creates an empty matplotlibrc file)

This did the trick, and my Windows python matplotlib's backend was left as Qt4Agg.


Here it is on one line for copy/paste:

cd /home/<user>/.config/matplotlib; touch matplotlibrc && echo "backend : Agg" > matplotlibrc
like image 57
binaryfunt Avatar answered Nov 03 '22 21:11

binaryfunt