Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm getting _tkinter.TclError: couldn't connect to display "localhost:10.0" when SSH'ing (with -X) into my Ubuntu 16.04 servers, running this script

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")

This script is using Wordcloud (https://github.com/amueller/word_cloud/). words.txt is a bunch of words that I intend to turn into a wordcloud. What's supposed to happen is the cloud gets saved as comments.png (but not displayed) on the server I'm ssh'ing into.

like image 984
W. Reyna Avatar asked May 06 '18 21:05

W. Reyna


1 Answers

I had the same problem working with Tensorflow using Ubuntu 16.04 via SSH.

Try using Agg rendering engine instead of X11 (it worked for me).

Adding the following lines did the trick

import matplotlib
matplotlib.use('Agg')

Thanks to @Mark from this thread: Problem running python/matplotlib in background after ending ssh session

like image 191
manuelbcd Avatar answered Nov 12 '22 22:11

manuelbcd