[Python] Runtimeerror:invalid DISPLAY Variable

Source: Internet
Author: User

1. Problem: Drawing locally with matplotlib is possible, but error runtimeerror:invalid DISPLAY when SSH remote drawing variable

2. Cause:The default backend of Matplotlib is Tkagg, while Fltkagg, GTK, Gtkagg, Gtkcairo, Tkagg, Wx or Wxagg These backend require GUI graphical interface, so when SSH operation will error.

Import is Interactive backend. Turning Interactive mode on.plt.get_backend () out[3]: u'tkagg'

3. Workaround: Specify a backend (Agg, Cairo, PS, PDF or SVG) that does not require a GUI

Import Matplotlib.pyplot as Pltplt.switch_backend ('agg')

4. Source

View the source code implementation and parsing of Plt.switch_backend ():

defswitch_backend (newbackend):"""Switch the default backend.  This feature are **experimental**, and is only expected to work switching to an image backend. e.g., if you have a bunch of PostScript scripts so want to run from the interactive Ipython session, you may WA  NT to switch to the PS backend before running them to avoid have a bunch of GUI windows popup.    If you try the interactively switch from the one GUI backend to the another, you'll explode.    Calling this command would close all open windows. """Close (' All')    Global_backend_mod, New_figure_manager, draw_if_interactive, _show Matplotlib.use (newbackend, Warn=false, force=True) fromMatplotlib.backendsImportPylab_setup _backend_mod, New_figure_manager, draw_if_interactive, _show= Pylab_setup ()

View source code for Matplotlib.use ():

defUse (ARG, warn=true, force=False):"""    Set the Matplotlib backend to one of the known backends. The argument is case-insensitive.    *WARN* Specifies whether a warning should be issued if a backend have already been set up. *force* is an **experimental** flag, tells Matplotlib to attempt to initialize a new backend by reloading the Backe    ND module. .. Note: This function must is called *before* importing Pyplot for the first time;  Or, if you is not using the pyplot, it must be called before importing Matplotlib.backends.  If warn is True, a warning are issued if you try and call this after Pylab or pyplot has been loaded.        In certain black magic use cases, e.g.  : func: ' Pyplot.switch_backend ', we is doing the reloading necessary to make the backend switch work (in some cases,    e.g., pure image backends) so one can set warn=false to suppress the warnings.     To find out which backend is currently set, See:func: ' Matplotlib.get_backend '."""    #Lets determine the proper backend name first    ifArg.startswith ('module://'): Name=ArgElse:        #lowercase only non-module backend names (modules is case-sensitive)arg =arg.lower () name=Validate_backend (ARG)#Check If we ' ve already set up a backend    if 'Matplotlib.backends' inchSys.modules:#Warn only if called with a different name        if(rcparams['Backend']! = name) andWarn:warnings.warn (_use_error_msg)#unless we ' ve been told to force it, just return        if  notForce :returnNeed_reload=TrueElse: Need_reload=False#Store the backend namercparams['Backend'] =name#If needed We reload here because a lot of Setup code are triggered on    #module Import. See backends/__init__.py for more detail.    ifNeed_reload:reload (sys.modules['Matplotlib.backends'])

Reference: DISPLAY error matplotlib http://chewpichai.blogspot.com/2008/01/display-error-matplotlib.html. The original text may be wall, need to try to turn over.

Original content:

DISPLAY Error Matplotlibwhen error occur about this"$DISPLAY not set"When you run Python code, use matplotlib this happened because your matplotlib backend isSet to Fltkagg, GTK, Gtkagg, Gtkcairo, Tkagg, WxorWxagg They required a GUI, why error occur. To solve the must specific other backend that notRequired GUI (Agg, Cairo, PS, PDForSVG) when use Matplotlib
In CodeImportmatplotlib Matplotlib.use ('Agg')
Command Line use-d Optionpython subplot_demo.py-Daggremember when call Savefig ('filename') Don't give it extension this would handle by backend so you specific e.g Agg would create file Filename.png

[Python] Runtimeerror:invalid DISPLAY Variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.