Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #12257 on branch v3.0.x (Document standard backends in matplotlib.use()) #12400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,16 +1323,23 @@ def use(arg, warn=True, force=False):
"""
Set the matplotlib backend to one of the known backends.

To find out which backend is currently set, see
:func:`matplotlib.get_backend`.


Parameters
----------
arg : str
The backend to switch to. This can either be one of the
'standard' backend names or a string of the form
``module://my.module.name``. This value is case-insensitive.
'standard' backend names:

- interactive backends:
GTK3Agg, GTK3Cairo, MacOSX, nbAgg,
Qt4Agg, Qt4Cairo, Qt5Agg, Qt5Cairo,
TkAgg, TkCairo, WebAgg, WX, WXAgg, WXCairo

- non-interactive backends:
agg, cairo, pdf, pgf, ps, svg, template

or a string of the form: ``module://my.module.name``.

Note: Standard backend names are case-insensitive here.

warn : bool, optional
If True, warn if this is called after pyplot has been imported
Expand All @@ -1344,7 +1351,10 @@ def use(arg, warn=True, force=False):
If True, attempt to switch the backend. This defaults to
False.


See Also
--------
:ref:`backends`
matplotlib.get_backend
"""
name = validate_backend(arg)

Expand Down Expand Up @@ -1383,7 +1393,13 @@ def use(arg, warn=True, force=False):


def get_backend():
"""Return the name of the current backend."""
"""
Return the name of the current backend.

See Also
--------
matplotlib.use
"""
return rcParams['backend']


Expand Down
21 changes: 21 additions & 0 deletions lib/matplotlib/tests/test_matplotlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import matplotlib
import matplotlib.rcsetup


def test_use_doc_standard_backends():
"""
Test that the standard backends mentioned in the docstring of
matplotlib.use() are the same as in matplotlib.rcsetup.
"""
def parse(key):
backends = []
for line in matplotlib.use.__doc__.split(key)[1].split('\n'):
if not line.strip():
break
backends += [e.strip() for e in line.split(',') if e]
return backends

assert (set(parse('- interactive backends:\n')) ==
set(matplotlib.rcsetup.interactive_bk))
assert (set(parse('- non-interactive backends:\n')) ==
set(matplotlib.rcsetup.non_interactive_bk))
3 changes: 3 additions & 0 deletions tutorials/introductory/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ def my_plotter(ax, data1, data2, param_dict):
# Again, for these simple examples this style seems like overkill, however
# once the graphs get slightly more complex it pays off.
#
#
# .. _backends:
#
# Backends
# ========
#
Expand Down