The World’s Largest Online Community for Developers

'; python - Matplotlib back-end does not work properly on WSL - LavOzs.Com

I'm struggling with a famous old problem with matplotlib's back-end (related posts here: (1), (2)).

Current setup:

  • conda 4.6.11 (python 3.7.3, matplotlib 3.0.3)
  • Ubuntu 18.04 (through WSL)

I'd tried a few ways to solve this issue (by using matplotlib.use("TkAgg") method call within my program, entering the ~/.config/matplotlib - if you are not familiar with this approaches, they are stated in the first post (1)), but nothing seemed to work.

Nonetheless, I have found the second post - (2) - and this looks like the most logical solution out there. As stated by Adrian Rosebrock in this post, in order to solve this problem, you have to (a) uninstall matplotlib with pip uninstall matplotlib, (b) install few libs from TkInter GUI library with sudo apt-get install tcl-dev tk-dev python-tk python3-tk and (c) reinstall matplotlib again with the fresh TkInter dependencies.

My question is: due to the fact that I'm using conda, if I proceed to uninstalling matplotlib with pip, are there any dangers of messing up another dependencies somewhere by doing this? On top of that, is matplotlib going to behave the same if it gets reinstalled with apt-get, or by manually git clone and running the setup?

Or, is there any way of fixing this issue I couldn't find?

The code I'm trying to run:

# Set up the back-end for the lib
import matplotlib
import matplotlib.pyplot as plt

plt.plot([0, 1, 2], [1, 2, 3])

I managed to find a solution to my issue (more of an alternative, than a proper solution, nevertheless, I think I know where the problem comes from and you should be aware of doing alike how I did).

As mentioned in the post, I'm using a Ubuntu 18.04 setup via WSL 2 on Windows 10. WSL is an environment in Windows 10 which supports running Linux native binaries and it is meant to be as lightweight as possible. That means it provides no complex GUI to the user, but only a terminal. For more info check this link.

I wrote my code in Visual Studio, which has an extension that integrates with WSL and basically connects to this VM. It confused me, because I was basically in Windows, writing code in my VSCode, but the code was running on this particular remote VM, through WSL. Another cool thing about this VM, is that even it has its own isolated filesystem, you can access the Windows' files through /mnt.

That being said, the reason matplotlib did not work as expected (more specifically the show() method provided no output) was in fact WSL. The Python application I've been trying to run was obviously running on WSL, but due to the WSL's nature, the output couldn't be visualized.

The fact that consolidates this belief is running the same code with the same setup (Python 3.7.4, Conda 4.6.11) directly from Windows 10, without any WSL, as ImportanceOfBeingErnest adviced me to do. Obviously, the code ran successfully, with the desired plot being given as output.

The alternative for WSL 2:

  • Due to the fact that you can access Windows files from /mnt, instead of trying to plot the figure with, just use the method plt.savefig("figure_name.png")
  • The .png is then saved to your computer and can be visualized on your Windows

Off-topic: Whether you prefer rolling with conda on your Windows or on WSL 1/2, I strongly recommend to use the virtual environments it provides, because they are versatile and lets you configure your Python libraries with ease.

How does Python's super() work with multiple inheritance?
In Matplotlib, what does the argument mean in fig.add_subplot(111)?
Why does range(start, end) not include end?
How does collections.defaultdict work?
matplotlib does not show my drawings although I call
How does the @property decorator work?
Why is pyplot giving me a TclError on osx?
Failure to import matplotlib.pyplot in jupyter (but not ipython)
pip install -e . gives error 13 in conda environment