python-3.x - _tkinter.TclError:使用 wordcloud 时无法连接以显示“localhost:10.0”
问题描述
_tkinter.TclError: couldn't connect to display "localhost:10.0"
当 SSH'ing (with ) 进入我-X
的 Ubuntu 16.04 服务器时,我正在运行这个脚本
from os import path
from wordcloud import WordCloud
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use('Agg')
d = path.dirname(__file__)
text = open(path.join(d, 'words.txt')).read()
wordcloud = WordCloud().generate(text)
# Configure plot
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.savefig("comments.png")
该脚本使用 Wordcloud ( https://github.com/amueller/word_cloud/ )。words.txt
是一堆我打算变成 wordcloud 的词。应该发生的是云在我正在进入的服务器上保存为 comments.png(但不显示)。
解决方案
我在通过 SSH 使用 Ubuntu 16.04 使用 Tensorflow 时遇到了同样的问题。
尝试使用 Agg 渲染引擎而不是 X11(它对我有用)。
添加以下几行就可以了
import matplotlib
matplotlib.use('Agg')
感谢来自该线程的@Mark:结束 ssh 会话后在后台运行 python/matplotlib 时出现问题
推荐阅读
- java - 已弃用的“GoogleCredential”的替代方案是什么?
- javascript - for循环中的克隆元素中断并永远运行
- python - 强制 pandas 将类型信息存储在输出文件中
- javascript - JavaScript 项目中的本地依赖项
- html - 如何跨行移动png
- mysql - 计数出现的总和
- xml - libxml2 对包含特殊字符(如“[”、“[”)的元素类型 anyURI 的 xsd 模式验证失败
- datepicker - Odoo 12:如何使用 datepicker 禁用过去的日期?
- javascript - React 中未处理的 Promise Rejection
- nginx - Ingress 和 nginx 配置问题