kwplot.auto_backends module

This module handles automatically determening a “good” matplotlib backend to use before importing pyplot.

kwplot.auto_backends.autompl(verbose=0, recheck=False, force=None)[source]

Uses platform heuristics to automatically set the matplotlib backend. If no display is available it will be set to agg, otherwise we will try to use the cross-platform Qt5Agg backend.

Parameters
  • verbose (int, default=0) – verbosity level

  • recheck (bool, default=False) – if False, this function will not run if it has already been called (this can save a significant amount of time).

  • force (str, default=None) – backend to force to or “auto”

CommandLine

# Checks
export QT_DEBUG_PLUGINS=1
xdoctest -m kwplot.auto_backends autompl --check
KWPLOT_UNSAFE=1 xdoctest -m kwplot.auto_backends autompl --check
KWPLOT_UNSAFE=0 xdoctest -m kwplot.auto_backends autompl --check

Example

>>> # xdoctest +REQUIRES(--check)
>>> plt = autoplt(verbose=1)
>>> plt.figure()

References

https://stackoverflow.com/questions/637005/check-if-x-server-is-running

kwplot.auto_backends.autoplt(verbose=0, recheck=False, force=None)[source]

Like autompl, but also returns the matplotlib.pyplot module for convenience.

Returns

ModuleType

kwplot.auto_backends.autosns(verbose=0, recheck=False, force=None)[source]

Like autompl, but also calls seaborn.set() and returns the seaborn module for convenience.

Returns

ModuleType

kwplot.auto_backends.set_mpl_backend(backend, verbose=None)[source]
Parameters

backend (str) – name of backend to use (e.g. Agg, PyQt)

class kwplot.auto_backends.BackendContext(backend)[source]

Bases: object

Context manager that ensures a specific backend, but then reverts after the context has ended.

Because this changes the backend after pyplot has initialized, there is a chance for odd behavior to occur. Please submit and issue if you experience this and can document the environment that caused it.

CommandLine

# Checks
xdoctest -m kwplot.auto_backends BackendContext --check

Example

>>> # xdoctest +REQUIRES(--check)
>>> from kwplot.auto_backends import *  # NOQA
>>> import matplotlib as mpl
>>> import kwplot
>>> print(mpl.get_backend())
>>> #kwplot.autompl(force='auto')
>>> #print(mpl.get_backend())
>>> #fig1 = kwplot.figure(fnum=3)
>>> #print(mpl.get_backend())
>>> with BackendContext('agg'):
>>>     print(mpl.get_backend())
>>>     fig2 = kwplot.figure(fnum=4)
>>> print(mpl.get_backend())