14

I am using pycharm with a remote interpreter.

When I try to use matplotlib I get the following error:

>>> import matplotlib.pyplot as plt
Backend TkAgg is interactive backend. Turning interactive mode on.
Failed to enable GUI event loop integration for 'tk'
Traceback (most recent call last):
  File "/home/donbeo/.pycharm_helpers/pydev/pydev_console_utils.py", line 498, in do_enable_gui
    enable_gui(guiname)
  File "/home/donbeo/.pycharm_helpers/pydev/pydev_ipython/inputhook.py", line 509, in enable_gui
    return gui_hook(app)
  File "/home/donbeo/.pycharm_helpers/pydev/pydev_ipython/inputhook.py", line 262, in enable_tk
    app = _TK.Tk()
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1808, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
>>> plt.plot([1,2,3])
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2821, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-e426dd61f8f7>", line 1, in <module>
    plt.plot([1,2,3])
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 2980, in plot
    ax = gca()
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 803, in gca
    ax =  gcf().gca(**kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 450, in gcf
    return figure()
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1808, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
>>> plt.show()

How can I solve?

Donbeo
  • 17,067
  • 37
  • 114
  • 188
  • I am facing similar issues. Where you able to solve your problems? – Maecky Oct 26 '15 at 11:22
  • I did not solve. I think you can save the plot in the ssh server `plt.savefig(...)` but `plt.show() ` returns an error. – Donbeo Oct 26 '15 at 12:34
  • I once goht it working with putty and xming. You have to enable x11 forwarding but I was not able to setup it in pycharm.... – Maecky Oct 26 '15 at 13:07
  • Have you solved the issue? `plt.savefig(...)` doesn't work... I only want to run some tests on a remote machine, save the plots as images and retrieve them later... – rhcpfan Jan 23 '16 at 19:54
  • At the end I log in the remote machine with terminal and ssh.Then I run emacs inside the terminal. It is good once you get used to it – Donbeo Jan 23 '16 at 20:25

2 Answers2

6

I had the same problem and fixed it by changing to a non-interactive backend:

import matplotlib
matplotlib.use('Agg')
lopsided
  • 2,370
  • 6
  • 28
  • 40
  • 1
    Did you need to use X11 display forwarding? – beldaz Feb 01 '18 at 05:25
  • 1
    No, you only need display forwarding if you want to render results onto your workstation from the remote machine. If you just want it not to crash then this is all you need to do. – lopsided Feb 01 '18 at 12:49
6

First, you need to forward X11 connections to your local machine (ssh -X ... for linux, for windows you can use VcXsrv and setup the forwarding in your ssh client).

Next, set the DISPLAY environment variable in your run configuration as described here: https://stackoverflow.com/a/32945380/2708478

After that, plt.show() will show the plot on your local machine.

Community
  • 1
  • 1
Alexey Romanov
  • 256
  • 3
  • 6