在Ubuntu中显示matplotlib图(Linux子系统的Windows子系统)
因此,它似乎在Ubuntu的Windows(Windows子系统为Linux)人们建议我们需要使用Agg后端,只保存图像,而不是显示情节。在Ubuntu中显示matplotlib图(Linux子系统的Windows子系统)
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
我们怎样才能把它放到plt.show()实际显示图像的位置?我目前的选择是重写plot.show(),而不是只保存在/ mnt/c/Users/james/plots /下的plot-148123456.png窗口中,只需打开浏览器窗口即可查看图像。
我想我可以托管该文件夹并使用浏览器。
我的目标是能够像上面的代码一样运行简单的示例,而不需要将代码更改为ftp图像等等。我只希望剧情出现在窗口中。
有没有人想出了一个体面的方式来做到这一点?
好吧,所以我得到它的工作如下,但一些步骤可能没有必要。我有Ubuntu的窗口上,安装了水蟒蟒3.6
- 谷歌 “的Xming”(X11服务器)和下载(从SourceForge)/安装/运行
sudo apt-get install x11-apps
-
export DISPLAY=localhost:0.0
(添加到~/.bashrc
使永久) -
sudo apt-get install gnome-calculator
(获得GTK) - 编辑
matplotlibrc
和改变后端:到后端:TkAgg
我matplotlibrc
在/home/james/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
注意GTKAgg
没有工作,所以第4步可能是毫无意义的,不知道。 gnome-calulator起作用,所以GTK似乎工作,但它说需要pygtk但pip install pygtk
没有工作。
不管怎么说,毕竟是,这个代码在WSL Ubuntu的运行工作作为是:
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()
```
我有一种感觉,这是更好通过Jupyter笔记本或其他东西完成,但很高兴在Linux上的子系统上的Ubuntu for Windows上具有基本的命令行python matplotlib功能
编辑:今天我运行了下面的命令,安装Qt5作为后端,并且它与Qt5Agg作为后端(vs TkAgg)正常工作。如果你正在运行的一些事情上WSL其他这就需要QT5
sudo apt-get update && sudo apt-get install qtbase5-dev
而且这可能是有帮助的,找到你的matplotlibrc,并命令提示符下键入:
python import matplotlib print(matplotlib.matplotlib_fname()) quit()
我只需要在windows端安装xming,并在ubuntu端安装python-tk sudo apt-get install python-tk,然后在设置DISPLAY之后很好。 – AlistairH
@AlistairH你把DISPLAY设置为了什么? – ScheissSchiesser
'DISPLAY = localhost:0.0',如上面的答案。 – AlistairH
要获得matplotlib在猛砸在Ubuntu与GTKAgg工作在Windows,我:
- 安装VcXsrv Windows下如上所述(但事情应该只是用的Xming相同)
- 设置的显示[
export DISPLAY=localhost:0.0
(添加到〜/ .bashrc中,使永久) - 执行
sudo pip uninstall matplotlib
- 其次
sudo apt install python-matplotlib
- 更新matplotlibrc读取
backend : GTKAgg
(而不是backend : agg
) - 我也跑了
sudo apt-get install python-gtk2-dev
,但这可能没有必要。
Uninstalling the pip-installed matplotlib and reinstalling it via apt似乎是必要的,因为pip不包括运行GTK所需的C扩展,但apt版本的确如此。
python文件的顶部:import matplotlib; matplotlib.use( 'GTKAgg');不要忘记启动VcXsrc。我跳过了第6步。谢谢! – Jason
@Jason:我更新了我的'matplotlibrc'(步骤5),以便默认使用'GTKAgg',而不是显式设置'matplotlib use'属性,但我喜欢你的变体。另外,关于验证VcXsvr正在运行的好处。很高兴我能帮上忙! – marisano
请注意'mobaXtrem'内置了xserver,可以用来代替安装'VcXsrv'。请参阅https://nickjanetakis.com/blog/using-wsl-and-mobaxterm-to-create-a-linux-dev-environment-on-windows – oak
在[这个问题](http://*.com/questions/40566837/no-plot-window-in-matplotlib-in-linux-shell-windows-10)答案表示,这将是可能的为此目的使用xming。你也可以考虑在SuperUser中提出一个类似的问题(不要过多地关注matplotlib)。 – ImportanceOfBeingErnest