56

I am running my python script in another machine by using ssh command in linux. I have also run this command :

source ~/.bashrc 

after logging in the other machine, in order to define the proper paths in the new machine. I was getting the error message for running the following python code lines even I have tried to follow the instruction in this question by defining the backend.

>>> import matplotlib
>>> import pylab as plt
>>> matplotlib.use('Agg')
>>> import numpy as np
>>> x=np.arange(0,2,0.001)
>>> y=np.sin(x)**2+4*np.cos(x)
>>> fig = plt.figure()
>>> plt.plot(x,y,'r.')     

The error message

This probably means that Tcl wasn't installed properly.
Traceback (most recent call last):
  File "Systematic_Optimised.py", line 513, in <module>
    fig = plt.figure()
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 435, in figure
    **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 47, in new_figure_manager
    return new_figure_manager_given_figure(num, thisFig)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 54, in new_figure_manager_given_figure
    canvas = FigureCanvasQTAgg(figure)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 72, in __init__
    FigureCanvasQT.__init__(self, figure)
  File "/vol/aibn84/data2/zahra/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 68, in __init__
    _create_qApp()
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

any suggestion how to fix the problem

Community
  • 1
  • 1
Dalek
  • 4,168
  • 11
  • 48
  • 100

2 Answers2

72

You must declare matplotlib.use('agg') before import pylab as plt.

Reference

Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
  • still I have the runtime error :( File "/home/user/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp raise RuntimeError('Invalid DISPLAY variable') RuntimeError: Invalid DISPLAY variable – Hana90 Apr 01 '17 at 21:10
  • 1
    Take a look at this [comment](https://github.com/matplotlib/matplotlib/issues/3466/#issuecomment-195899517) – Mauro Baraldi Apr 04 '17 at 16:17
  • 1
    it seems you should use lower case 'a', so do matplotlib.use('agg') before import pylab as plt. – Michelle Owen Jun 08 '17 at 14:12
  • @MichelleOwen have you tested? I cannot test it now! – Mauro Baraldi Jun 08 '17 at 14:20
  • 1
    @MauroBaraldi yes, I did. At least it worked for me. – Michelle Owen Jun 09 '17 at 15:52
  • 1
    worked for me too in a virtual environment after having to `pip install PySide`. Thanks a lot! – fr_andres Jun 12 '17 at 19:31
  • 13
    For me, `import matplotlib; matplotlib.use('agg')` didn't fix the error, but `import matplotlib; matplotlib.pyplot.switch_backend('agg')` did (as suggested [here](https://github.com/matplotlib/matplotlib/issues/3466/#issuecomment-270603717)). – teichert Jul 06 '17 at 20:08
56

Add

plt.switch_backend('agg')

after

import matplotlib.pyplot as plt
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
ssmetkar
  • 559
  • 4
  • 3
  • Thanks! This plays much more nicely with pylint :) – Hugh Perkins Jun 04 '18 at 00:06
  • 3
    This is fantastic, now I can switch the backends depending on how I run the code. I have a problem when using 'agg' which stops showing the plot in the command window which can sometimes be disappointing. Therefore, I can switch out the backend for my uses. – pacificgilly1992 Aug 03 '18 at 13:18