So it seems on ubuntu for windows (windows subsystem for linux) people are suggesting we need to use Agg backend and just save images, not show plots.
import matplotlib
matplotlib.use('Agg') # no UI backend
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()
plt.savefig("matplotlib.png") #savefig, don't show
How could we get it to where plt.show() would actually show us an image? My current option is to override plot.show() to instead just savefig a plot-148123456.png under /mnt/c/Users/james/plots/ in windows and just have an explorer window open viewing the images.
I suppose I could host that folder and use a browser.
My goal is to be able to run simple examples like the code above without changing the code to ftp the images somewhere etc. I just want the plot to show up in a window.
Has anyone figured out a decent way to do it?
Ok, so I got it working as follows. I have Ubuntu on windows, with anaconda python 3.6 installed.
- Download and install Xming (X11 for Windows) from sourceforge or VcXsrv (see edit below)
sudo apt-get update
sudo apt-get install python3.6-tk
(you may have to install a differentpython*-tk
depnding on the python version you're using)pip install matplotlib
(for matplotlib. but many other things now work too)export DISPLAY=localhost:0.0
(add to~/.bashrc
to make permanent)
Anyways, after all that, this code running in ubuntu on wsl worked as is:
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()
Maybe this is better done through a Jupyter notebook or something, but it's nice to have basic command-line python matplotlib functionality in Ubuntu for Windows on Subsystem for Linux, and this makes many other gui apps work too.
Edit 2019-09-04 : Today I was having issues with 'unable to get screen resources' after upgrading some libraries. So I installed VcXsrv and used that instead of Xming. Just install from https://sourceforge.net/projects/vcxsrv/ and run xlaunch.exe, select multiple windows, next next next ok. Then everything worked.
To get matplotlib to work with GTKAgg on Bash on Ubuntu on Windows, I:
- installed VcXsrv under Windows (but things should work just the same with Xming)
- set DISPLAY as noted above [
export DISPLAY=localhost:0.0
(add to ~/.bashrc to make permanent)] - executed
sudo pip uninstall matplotlib
- followed by
sudo apt install python-matplotlib
- updated matplotlibrc to read
backend : GTKAgg
(rather thanbackend : agg
) - I also ran
sudo apt-get install python-gtk2-dev
, but this may not be necessary.
Uninstalling the pip-installed matplotlib and reinstalling it via apt appear to be necessary because pip does not include the C extensions needed to run GTK, but the apt version does.
I found the best approach is to install Jupyter on Windows Subsystem for Linux (WSL) by following
sudo apt update && upgrade
sudo apt install python3 python3-pip ipython3
Now you can install matplotlib
pip3 install matplotlib
And Jupyter Notebook
pip3 install jupyter
Check this link if you need more info Python setup on the Windows subsystem for Linux (WSL)
来源:https://stackoverflow.com/questions/43397162/show-matplotlib-plots-in-ubuntu-windows-subsystem-for-linux