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

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

问题描述

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

  import 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,并安装了蟒蛇python 3.6.

  1. 下载并安装

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

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

    编辑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. 该IP不是本地主机.它在resolv.conf中,所以请改为运行它(并包含在〜/.bashrc中):

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

    1. 现在仔细检查防火墙:Windows安全->防火墙与网络保护->允许应用通过防火墙->确保VcXsrv同时选中了public和private.(首次启动xlaunch时,可能会提示您允许通过防火墙.这也可行.此外,如果VcXsrv不在应用程序列表中,则可以手动添加它,例如,从'C:\ program files \ vcxsrv \ vcxsrv.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并将其用于DISPLAY.对于大多数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 files\vcxsrv\vcxsrv.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和WSL2)中显示matplotlib图(和其他GUI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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