在Ubuntu(适用于Linux的Windows子系统)中显示matplotlib图 [英] Show matplotlib plots in Ubuntu (Windows subsystem for Linux)

查看:659
本文介绍了在Ubuntu(适用于Linux的Windows子系统)中显示matplotlib图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我们如何才能到达plt.show()实际向我们显示图像的位置?我当前的选择是覆盖plot.show(),而不是只在Windows中的/mnt/c/Users/james/plots/下保存一个plot-148123456.png并打开浏览器窗口以查看图像.

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.

我的目标是能够运行简单的示例,例如上面的代码,而无需更改代码以将ftp图像传输到某处,等等.我只希望该图显示在窗口中.

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?

推荐答案

好,所以我按如下方式工作.我在Windows上安装了Ubuntu,并安装了蟒蛇python 3.6.

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

  1. 从sourceforge或 Xming (适用于Windows的X11): //sourceforge.net/projects/vcxsrv/"rel =" noreferrer> VcXsrv (请参见下面的编辑)
  2. sudo apt-get update
  3. sudo apt-get install python3.6-tk(您可能需要在使用的python版本上安装不同的python*-tk)
  4. pip install matplotlib(适用于matplotlib.但现在其他许多功能也可以使用)
  5. export DISPLAY=localhost:0.0(添加到~/.bashrc以使其永久保存)
  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)

无论如何,毕竟,在wsl的ubuntu中运行的这段代码按原样工作:

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()

结果:

也许通过Jupyter笔记本或其他工具可以更好地完成此操作,但是很高兴在Linux子系统上的Windows版Ubuntu中具有基本的命令行python matplotlib功能,这也使许多其他gui应用程序也可以工作.

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.

编辑2019-09-04 :今天,在升级某些库后,我遇到了无法获取屏幕资源"的问题.因此,我安装了VcXsrv并使用它代替了Xming.只需从 https://sourceforge.net/projects/vcxsrv/安装并运行xlaunch.exe,选择多个窗口,下一个下一个好的.然后一切正常.

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.

这篇关于在Ubuntu(适用于Linux的Windows子系统)中显示matplotlib图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆