-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
allow selecting the backend by setting the environment variable MPLBACKEND #3710
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
Changes from all commits
0d995d4
0ead4b2
fffa9c1
7290dc1
0042453
40e4019
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,19 +302,52 @@ pygtk, wxpython, tkinter, qt4, or macosx; also referred to as | |
"interactive backends") and hardcopy backends to make image files | ||
(PNG, SVG, PDF, PS; also referred to as "non-interactive backends"). | ||
|
||
There are a two primary ways to configure your backend. One is to set | ||
the ``backend`` parameter in your ``matplotlibrc`` file (see | ||
:ref:`customizing-matplotlib`):: | ||
There are a four ways to configure your backend. If they conflict each other, | ||
the method mentioned last in the following list will be used, e.g. calling | ||
:func:`~matplotlib.use()` will override the setting in your ``matplotlibrc``. | ||
|
||
backend : WXAgg # use wxpython with antigrain (agg) rendering | ||
|
||
The other is to use the matplotlib :func:`~matplotlib.use` directive:: | ||
#. The ``backend`` parameter in your ``matplotlibrc`` file (see | ||
:ref:`customizing-matplotlib`):: | ||
|
||
import matplotlib | ||
matplotlib.use('PS') # generate postscript output by default | ||
backend : WXAgg # use wxpython with antigrain (agg) rendering | ||
|
||
If you use the ``use`` directive, this must be done before importing | ||
:mod:`matplotlib.pyplot` or :mod:`matplotlib.pylab`. | ||
#. Setting the :envvar:`MPLBACKEND` environment | ||
variable, either for your current shell or for a single script:: | ||
|
||
> export MPLBACKEND="module://my_backend" | ||
> python simple_plot.py | ||
|
||
> MPLBACKEND="module://my_backend" python simple_plot.py | ||
|
||
Setting this environment variable will override the ``backend`` parameter | ||
in *any* ``matplotlibrc``, even if there is a ``matplotlibrc`` in your | ||
current working directory. Therefore setting :envvar:`MPLBACKEND` | ||
globally, e.g. in your ``.bashrc`` or ``.profile``, is discouraged as it | ||
might lead to counter-intuitive behavior. | ||
|
||
#. To set the backend for a single script, you can alternatively use the `-d` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am ambivalent about advertising this feature. I do see the warning below, but I'd much rather go the route of deprecating this feature in favor of the environment variable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah -- maybe we should start the deprecation process for this now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the idea of completely leaving
Should using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes please On Tue, Nov 18, 2014 at 11:29 AM, Florian Rhiem notifications@github.com
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should probably raise a mplDeprecation warning. Like #3812 |
||
command line argument:: | ||
|
||
> python script.py -dbackend | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space |
||
|
||
This method is **deprecated** as the `-d` argument might conflict with | ||
scripts which parse command line arguments (see issue | ||
`#1986 <https://github.com/matplotlib/matplotlib/issues/1986>`_). You | ||
should use :envvar:`MPLBACKEND` instead. | ||
|
||
#. If your script depends on a specific backend you can use the | ||
:func:`~matplotlib.use` function:: | ||
|
||
import matplotlib | ||
matplotlib.use('PS') # generate postscript output by default | ||
|
||
If you use the :func:`~matplotlib.use` function, this must be done before | ||
importing :mod:`matplotlib.pyplot`. Calling :func:`~matplotlib.use` after | ||
pyplot has been imported will have no effect. Using | ||
:func:`~matplotlib.use` will require changes in your code if users want to | ||
use a different backend. Therefore, you should avoid explicitly calling | ||
:func:`~matplotlib.use` unless absolutely necessary. | ||
|
||
.. note:: | ||
Backend name specifications are not case-sensitive; e.g., 'GTKAgg' | ||
|
@@ -324,8 +357,8 @@ With a typical installation of matplotlib, such as from a | |
binary installer or a linux distribution package, a good default | ||
backend will already be set, allowing both interactive work and | ||
plotting from scripts, with output to the screen and/or to | ||
a file, so at least initially you will not need to use either of the | ||
two methods given above. | ||
a file, so at least initially you will not need to use any of the | ||
methods given above. | ||
|
||
If, however, you want to write graphical user interfaces, or a web | ||
application server (:ref:`howto-webapp`), or need a better | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,7 +180,7 @@ def _forward_ilshift(self, other): | |
|
||
# cbook must import matplotlib only within function | ||
# definitions, so it is safe to import from it here. | ||
from matplotlib.cbook import is_string_like | ||
from matplotlib.cbook import is_string_like, mplDeprecation | ||
from matplotlib.compat import subprocess | ||
|
||
try: | ||
|
@@ -1373,9 +1373,21 @@ def tk_window_focus(): | |
if s.startswith(str('-d')) and len(s) > 2: # look for a -d flag | ||
try: | ||
use(s[2:]) | ||
warnings.warn("Using the -d command line argument to select a " | ||
"matplotlib backend is deprecated. Please use the " | ||
"MPLBACKEND environment variable instead.", | ||
mplDeprecation) | ||
break | ||
except (KeyError, ValueError): | ||
pass | ||
# we don't want to assume all -d flags are backends, e.g., -debug | ||
else: | ||
# no backend selected from the command line, so we check the environment | ||
# variable MPLBACKEND | ||
try: | ||
use(os.environ['MPLBACKEND']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mdboom, just as a sanity check, are we going to have to worry at all about string/unicode issues here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. It was environment variables that contain Unicode paths that were giving us grief. In this case both the key and value should be ascii. (It's possible that someone might use unicode in the "module://my_custom_backend" form, but doing that on Python 2 would be insane/impossible for so many other reasons, it's not worth worrying about). |
||
except (KeyError, ValueError): | ||
pass | ||
|
||
default_test_modules = [ | ||
'matplotlib.tests.test_agg', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you put the space back in there? These docs are for reading, not typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the code, now I wonder if it ever worked with the space in there...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WeatherGod At least it does not work with module-backends, because the argument without the first two characters is passed to
use()
anduse()
containsarg.startswith('module://')
without a previous.strip()
call.