Show matplotlib plots in Ubuntu (Windows subsystem for Linux)

十年热恋 提交于 2019-12-03 18:20:47

问题


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?


回答1:


Ok, so I got it working as follows. I have Ubuntu on windows, with anaconda python 3.6 installed.

  1. Download and install Xming (X11 for Windows) from sourceforge or VcXsrv (see edit below)
  2. sudo apt-get update
  3. sudo apt-get install python3.6-tk (you may have to install a different python*-tk depnding on the python version you're using)
  4. pip install matplotlib (for matplotlib. but many other things now work too)
  5. 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()

result:

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.




回答2:


To get matplotlib to work with GTKAgg on Bash on Ubuntu on Windows, I:

  1. installed VcXsrv under Windows (but things should work just the same with Xming)
  2. set DISPLAY as noted above [export DISPLAY=localhost:0.0 (add to ~/.bashrc to make permanent)]
  3. executed sudo pip uninstall matplotlib
  4. followed by sudo apt install python-matplotlib
  5. updated matplotlibrc to read backend : GTKAgg (rather than backend : agg)
  6. 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.




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!