首页 > 解决方案 > _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(但不显示)。

标签: python-3.xubuntu

解决方案


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

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

添加以下几行就可以了

import matplotlib
matplotlib.use('Agg')

感谢来自该线程的@Mark:结束 ssh 会话后在后台运行 python/matplotlib 时出现问题


推荐阅读