linux matplotlib使用报错

在linux下使用matplotlib展示图像出现以下报错:

tkinter.TclError: no display name and no $DISPLAY environment variable

官网解释:

Many users report initial problems trying to use maptlotlib in web application servers, because by default matplotlib ships configured to work with a graphical user interface which may require an X11 connection. Since many barebones application servers do not have X11 enabled, you may get errors if you don’t configure matplotlib for use in these environments. Most importantly, you need to decide what kinds of images you want to generate (PNG, PDF, SVG) and configure the appropriate default backend. For 99% of users, this will be the Agg backend, which uses the C++ antigrain rendering engine to make nice PNGs

不同的系统有不同的用户图形接口,matplotlib默认的接口在windows下跑是没有问题的。一般在linux上面,要更改 matplotlib的默认配置,把模式更改成Agg。
两种解决办法:

  1. 在python文件中 ,import pyplot之前设置Agg
# do this before importing pylab or pyplot
Import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot asplt
  1. 在配置文件中修改backend
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc

我们也可以通过plt.savefig(filename)保存图片,最后在合适的环境中查看。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值