在 Ubuntu (WSL1 & WSL2) 中显示 matplotlib 图(和其他 GUI) [英] Show matplotlib plots (and other GUI) in Ubuntu (WSL1 & WSL2)

查看:200
本文介绍了在 Ubuntu (WSL1 & WSL2) 中显示 matplotlib 图(和其他 GUI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

导入 matplotlibmatplotlib.use('Agg') # 没有 UI 后端导入 matplotlib.pyplot 作为 plt将 numpy 导入为 npt = np.arange(0.0, 2.0, 0.01)s = 1 + np.sin(2*np.pi*t)plt.plot(t, s)plt.title('就这么简单,伙计们')#plt.show()plt.savefig("matplotlib.png") #savefig,不显示

我们怎样才能把它放到 plt.show() 实际向我们显示图像的地方?我目前的选择是覆盖 plot.show() 而不是在 Windows 中的/mnt/c/Users/james/plots/下保存一个 plot-148123456.png 并且只打开一个浏览器窗口查看图像.

我想我可以托管该文件夹并使用浏览器.

我的目标是能够运行像上面的代码这样的简单示例,而无需将代码更改为 ftp 图像某处等.我只希望绘图显示在窗口中.

有没有人想出一个体面的方法来做到这一点?

解决方案

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

  1. 下载并安装

    也许这最好通过 Jupyter notebook 或其他东西来完成,但在 Ubuntu for Windows 和 Subsystem for Linux 中拥有基本的命令行 python matplotlib 功能很好,这使得许多其他 gui 应用程序也可以工作.

    例如,您可以安装 xeyes,它会说安装 x11-apps 并安装它将安装许多 GUI 应用程序使用的 GTK.但关键是一旦您正确设置了 DISPLAY,并且您的 x 服务器在 Windows 上,那么大多数可以在本机 ubuntu 上运行的东西都可以用于 WSL.

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

    为 WSL 2 用户编辑 2020-06-23WSL2(目前是 Insider fast ring)有 GPU/docker 支持,所以值得升级.但是它在 vm 中运行.对于 WSL 2,请按照相同的步骤 1-4 然后:

    1. IP 不是本地主机.它在 resolv.conf 中,所以改为运行它(并包含在 ~/.bashrc 中):

     export DISPLAY=`grep -oP "(?<=nameserver ).+";/etc/resolv.conf`:0.0

    1. 现在仔细检查防火墙:Windows 安全 ->防火墙网络保护 ->允许应用程序通过防火墙 ->确保 VcXsrv 已检查公共和私人.(第一次启动 xlaunch 时,您可能会收到允许通过防火墙的提示.这也有效.此外,如果 VcXsrv 不在应用程序列表中,您可以手动添加它,例如从C:program filesvcxsrvvcxsrv.exe')
    2. 使用禁用访问控制"启动 VcXsrv打勾

    注意:一些 WSL2 用户收到错误,如无法连接到显示172.x.x.x:0".如果是这样,您可以尝试使用以下命令检查 DISPLAY 中存储的 IP 地址:echo $DISPLAY.如果显示的 IP 似乎是错误的(即8.8.8.8"或另一个不工作的 IP 地址),您需要将第 5 点中显示的 ~/.bashrc 中的代码更改为将获得您的实例的 IP 地址.一位用户说这有效:export DISPLAY=$(ifconfig | grep inet | awk '{print $2}' | head -n 1 | awk '{print $0":0"}').然而,对于其他一些人来说,它不起作用.YMMV,但只需找到您的 IP 并使用 if 进行显示.对于大多数 WSL2 用户,#5 中的命令有效.

    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?

    解决方案

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

    1. Download and install VcXsrv or Xming (X11 for Windows) from sourceforge(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. see WSL2 below)

    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.

    For example you can install xeyes, and it will say to install x11-apps and installing that will install GTK which a lot of GUI apps use. But the point is once you have your DISPLAY set correctly, and your x server on windows, then most things that would work on a native ubuntu will work for the WSL.

    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.

    Edit for WSL 2 users 2020-06-23 WSL2 (currently insider fast ring) has GPU/docker support so worth upgrade. However it runs in vm. For WSL 2, follow same steps 1-4 then:

    1. the ip is not localhost. it's in resolv.conf so run this instead (and include in ~/.bashrc):

     export DISPLAY=`grep -oP "(?<=nameserver ).+" /etc/resolv.conf`:0.0
    

    1. Now double-check firewall: Windows Security -> Firewall & network protection -> Allow an app through firewall -> make sure VcXsrv has both public and private checked. (When Launching xlaunch first time, you might get a prompt to allow through firewall. This works too. Also, if VcXsrv is not in list of apps, you can manually add it, eg from 'C:program filesvcxsrvvcxsrv.exe')
    2. Launch VcXsrv with "Disable access control" ticked

    Note: a few WSL2 users got error like couldn't connect to display "172.x.x.x:0". If that's you try to check the IP address stored in DISPLAY with this command: echo $DISPLAY. If the showed IP seems to be wrong (i.e. "8.8.8.8" or another not working IP address) you need to change the code in ~/.bashrc showed in the point 5 to something that will get your instance's ip address. One user said this worked: export DISPLAY=$(ifconfig | grep inet | awk '{print $2}' | head -n 1 | awk '{print $0":0"}'). However for some others it did not work. YMMV, but just find your IP and use if for DISPLAY. For most WSL2 users, the command in #5 works.

    这篇关于在 Ubuntu (WSL1 &amp; WSL2) 中显示 matplotlib 图(和其他 GUI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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