当通过 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/