1

我正在编写一个应该绘制一个简单函数的代码,它暂时可以工作,但是当我重新启动计算机时,我一直遇到这个问题循环:

  1. 我第一次尝试运行代码时,它不会发出任何错误,但也不会创建任何图表。

--> 为了解决这个问题,我按照AwokeKnowing的建议安装了Xmingexport DISPLAY=localhost:0.0并在 bash 中编写了命令Show matplotlib plots in Ubuntu (Windows subsystem for Linux) 中所建议的那样。

  1. 当我运行代码时,通过上述调整,我得到以下错误:
_tkinter.TclError: no display name and no $DISPLAY environment variable.

--> 为了解决这个问题,我添加了以下代码行:

matplotlib.use('Agg')

正如Serenity_tkinter.TclError 中提出的:没有显示名称和没有 $DISPLAY 环境变量

  1. 执行此操作并运行代码后,最初会正确绘制图形。但如果我改天再试一次,它不会。代码运行正常,但没有显示图表。

--> 为了让它工作,我删除了这行代码:

matplotlib.use('Agg')

通过这样做,代码再次绘制图表。

然后,当我重新启动计算机时,一系列问题又重新开始了。

有谁知道我做错了什么?我对使用 Python 很陌生,所以我很可能遗漏了一些明显的东西。

以下是我的代码的相关部分:

#Imports
import matplotlib
import matplotlib.pyplot as ply
from dolfin import *
import numpy as np
from mshr import *
import math
from math import exp

plt.plot(tiemporeal,fcmM1)
plt.xlabel('Tiempo')
plt.ylabel('Resistencia')
plt.show()

非常感谢,对可能的格式错误深表歉意。

PS。我在 Ubuntu 上使用 Python3。

4

1 回答 1

1

对于旧版 WSL,添加就足够了

export DISPLAY=127.0.0.1:0

到您的~/.bashrc文件 - 默认后端应该可以正常工作。对于 WSL 2,它更复杂,您需要从中获取服务器的名称,/etc/resolv.conf然后从ifconfig. 例如在我的系统上:

wmiller@lcl:~$ cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file,
# add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.28.176.1

wmiller@lcl:~$ ifconfig | grep 'inet '
        inet 172.28.176.7  netmask 255.255.240.0  broadcast 172.28.191.255 
        inet 127.0.0.1  netmask 255.0.0.0

所以我DISPLAY需要172.28.176.7:240.0。自动提取这有点复杂,但添加以下命令~/.bashrc对我有用:

export DISPLAY=$((ifconfig | grep -f <(cat /etc/resolv.conf | grep nameserver |
                  awk -F'[. ]' '{print $2"."$3}') | awk '{for(i=1; i <=NF; i++) 
                  {if($i == "inet") print $(i+1)}}' ; ifconfig | 
                  grep -f <(cat /etc/resolv.conf | grep nameserver | 
                  awk -F'[. ]' '{print $2"."$3}') | 
                  awk '{for(i=1; i <=NF; i++) {if($i == "netmask") print $(i+1)}}' | 
                  awk -F'.' '{print $3"."$4}') 
                 | tr "\n" " " | awk '{print $1":"$2}')

在任何一种情况下,您可能还需要在 xserver 客户端中禁用访问控制 - 我不确定 Xming 但 vcxsrv 只需要命令行参数-ac或在启动期间检查禁用访问控制。您还需要确保 Windows 防火墙允许连接。您可能会发现此线程很有用。

另请注意,这'Agg'是一个非 gui 后端,使用它不会显示数字。我发现我的设置'TkAgg'效果最好。

于 2020-03-30T00:16:41.033 回答