【解决方案】pyspark 绘图报错:_tkinter.TclError: no display name and no $DISPLAY environment variable

问题描述

matplotlib画图失败(pyspark、pyspark3),报错如下:

no display name and no $DISPLAY environment variable
Traceback (most recent call last):
  File "<stdin>", line 21, in plot_with_labels
  File "/usr/install/anaconda3-spark/lib/python3.6/site-packages/matplotlib/pyplot.py", line 539, in figure
    **kwargs)
  File "/usr/install/anaconda3-spark/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 171, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/usr/install/anaconda3-spark/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 1049, in new_figure_manager_given_figure
    window = Tk.Tk(className="matplotlib")
  File "/usr/install/anaconda3-spark/lib/python3.6/tkinter/__init__.py", line 2017, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

解决方案

利用matplotlib画图需要设置 plt.switch_backend('agg')

显示图像用魔法命令 %matplot plt 需要单独 cell 执行

示例代码

import matplotlib.pyplot as plt
plt.switch_backend("agg")
import numpy as np
x = np.arange(9)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,marker="*",linewidth=3,linestyle="--",color="orange")
plt.plot(x,z)
plt.title("matplotlib")
plt.xlabel("height")
plt.ylabel("width")
plt.legend(["Y","Z"],loc="upper right")
plt.grid(True)

在这里插入图片描述

参考链接:https://stackoverflow.com/questions/3453188/matplotlib-display-plot-on-a-remote-machine

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值