python – 在Ubuntu中显示matplotlib图(用于Linux的Windows子系统)

因此,似乎在ubuntu for windows(Linux子系统的Linux)上,人们建议我们需要使用Agg后端并只保存图像,而不是显示图.import matplotlibmatplotlib.use(Agg) # no UI backendimport matplotlib.pyplot as pltimport n...

因此,似乎在ubuntu for windows(Linux子系统的Linux)上,人们建议我们需要使用Agg后端并只保存图像,而不是显示图.

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,安装了anaconda python 3.6.

>从sourceforge下载并安装Xming(适用于Windows的X Window服务器)
> sudo apt-get update
> sudo apt-get install python3.6-tk(你可能需要在你正在使用的python版本上安装不同的python * -tk depnding)
> pip install matplotlib(对于matplotlib.但现在很多其他的东西也可以工作)
> export DISPLAY = localhost:0.0(添加到?/ .bashrc以使其永久化)

无论如何,毕竟在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功能很好,这使得许多其他gui应用程序也可以工作.

本文标题为:python – 在Ubuntu中显示matplotlib图(用于Linux的Windows子系统)

基础教程推荐