Show matplotlib plots in Ubuntu (Windows subsystem for Linux, WSL1 & WSL2)
因此,似乎在Windows的ubuntu(Linux的Windows子系统)上,人们建议我们需要使用Agg后端并只保存图像,而不显示图。
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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(),而不是只在Windows中的/ mnt / c / Users / james / plots /下保存一个plot-148123456.png并在浏览器窗口中打开以查看图像。
我想我可以托管该文件夹并使用浏览器。
我的目标是能够运行像上面的代码这样的简单示例,而无需更改代码以将ftp图像传输到某处,等等。我只希望该图显示在窗口中。
有没有人找到一种体面的方式做到这一点?
好的,所以我按如下方式工作。我在Windows上安装了Ubuntu,并安装了Pythonpython 3.6。
无论如何,毕竟,在wsl的ubuntu中运行的这段代码按原样工作:
1 2 3 4 5 6 7 8 9 | 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子系统上的Windows版Ubuntu上具有基本的命令行python matplotlib功能非常好,这也使许多其他gui应用程序也可以工作。
编辑2019-09-04:今天我在升级某些库后遇到``无法获取屏幕资源''的问题。所以我安装了VcXsrv并使用它代替了Xming。只需从https://sourceforge.net/projects/vcxsrv/安装并运行xlaunch.exe,选择多个窗口,下一步下一步即可。然后一切正常。
为WSL 2用户编辑2020-06-23
WSL2(当前是内部快速环)具有GPU / docker支持,因此值得升级。但是它在vm中运行。对于WSL 2,请遵循相同的步骤1-4,然后:
1 | export DISPLAY=`grep -oP"(?<=nameserver ).+" /etc/resolv.conf`:0.0 |
要使matplotlib在Windows上的Ubuntu的Bash上与GTKAgg一起使用,我:
似乎有必要卸载通过pip安装的matplotlib并通过apt重新安装,因为pip不包括运行GTK所需的C扩展,但是apt版本包含。
我发现最好的方法是通过以下步骤在Windows Linux子系统(WSL)上安装Jupyter
1 2 | sudo apt update && upgrade sudo apt install python3 python3-pip ipython3 |
现在您可以安装matplotlib
1 | pip3 install matplotlib |
和Jupyter笔记本
1 | pip3 install jupyter |
如果需要更多信息,请检查此链接,有关Linux(WSL)的Windows子系统上的Python设置的信息