【问题标题】:_tkinter.TclError: couldn't connect to display "localhost:10.0" when using wordcloud_tkinter.TclError:使用 wordcloud 时无法连接以显示“localhost:10.0”
【发布时间】:2018-10-16 16:34:03
【问题描述】:

当我通过 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 是一堆我打算变成 wordcloud 的词。应该发生的事情是云在我要进入的服务器上保存为 cmets.png(但不显示)。

【问题讨论】:

    标签: python-3.x ubuntu


    【解决方案1】:

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

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

    添加以下几行就可以了

    import matplotlib
    matplotlib.use('Agg')
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2015-12-30
      相关资源
      最近更新 更多