python-3.x - _tkinter.TclError : couldn't connect to display "localhost:10.0" when using wordcloud

标签 python-3.x ubuntu

当通过 SSH(使用 -X)进入我的 Ubuntu 16.04 服务器时,我得到 _tkinter.TclError: couldn't connect to display "localhost:10.0" , 运行这个脚本

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 是一堆我打算变成词云的词。应该发生的是云在我正在进入的服务器上被保存为 comments.png(但不显示)。

最佳答案

我在通过 SSH 使用 Ubuntu 16.04 使用 Tensorflow 时遇到了同样的问题。

尝试使用 Agg 渲染引擎而不是 X11(它对我有用)。

添加以下几行就可以了

import matplotlib
matplotlib.use('Agg')

感谢来自此线程的@Mark:Problem running python/matplotlib in background after ending ssh session

关于python-3.x - _tkinter.TclError : couldn't connect to display "localhost:10.0" when using wordcloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50204556/

相关文章:

python - 在不丢失数据的情况下更新字典

python - 打印枚举类的所有成员名称

c# - Mono Socket.Bind 在连接之前绑定(bind)以使用特定接口(interface)

laravel - 在 Ubuntu 上使用 Sail 和 PhpStorm 在 Laravel 8 上运行 Xdebug

c++ -/usr/include/x86_64-linux-gnu/bits/string_fortified.h :106:10: error: ‘__builtin_strncpy’

Python: 'NoneType' 对象没有属性 'get_username'

python - 如何优雅地发出自定义断言错误?

python - Tkinter 按钮与网格对齐

ubuntu - 如何使用 FFMPEG 将 MP4(H264、AAC)转换为质量损失为零的 WebM?

ubuntu - Jenkins 进程