@@ -1301,74 +1301,63 @@ def __exit__(self, exc_type, exc_value, exc_tb):
1301
1301
dict .update (rcParams , self ._orig )
1302
1302
1303
1303
1304
- _use_error_msg = """
1305
- This call to matplotlib.use() has no effect because the backend has already
1306
- been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
1307
- or matplotlib.backends is imported for the first time.
1308
-
1309
- The backend was *originally* set to {backend!r} by the following code:
1310
- {tb}
1311
- """
1312
-
1313
-
1314
1304
def use (arg , warn = True , force = False ):
1315
1305
"""
1316
1306
Set the matplotlib backend to one of the known backends.
1317
1307
1318
- The argument is case-insensitive. *warn* specifies whether a
1319
- warning should be issued if a backend has already been set up.
1320
- *force* is an **experimental** flag that tells matplotlib to
1321
- attempt to initialize a new backend by reloading the backend
1322
- module.
1308
+ To find out which backend is currently set, see
1309
+ :func:`matplotlib.get_backend`.
1310
+
1311
+
1312
+ Parameters
1313
+ ----------
1314
+ arg : str
1315
+ The backend to switch to. This can either be one of the
1316
+ 'standard' backend names or a string of the form
1317
+ ``module://my.module.name``. This value is case-insensitive.
1323
1318
1324
- .. note::
1319
+ warn : bool, optional
1320
+ If True, warn if this is called after pyplot has been imported
1321
+ and a backend is set up.
1325
1322
1326
- This function must be called *before* importing pyplot for
1327
- the first time; or, if you are not using pyplot, it must be called
1328
- before importing matplotlib.backends. If warn is True, a warning
1329
- is issued if you try and call this after pylab or pyplot have been
1330
- loaded. In certain black magic use cases, e.g.
1331
- :func:`pyplot.switch_backend`, we are doing the reloading necessary to
1332
- make the backend switch work (in some cases, e.g., pure image
1333
- backends) so one can set warn=False to suppress the warnings.
1323
+ defaults to True
1324
+
1325
+ force : bool, optional
1326
+ If True, attempt to switch the backend. This defaults to
1327
+ false and using `.pyplot.switch_backend` is preferred.
1334
1328
1335
- To find out which backend is currently set, see
1336
- :func:`matplotlib.get_backend`.
1337
1329
1338
1330
"""
1339
- # Lets determine the proper backend name first
1340
- if arg . startswith ( 'module://' ):
1341
- name = arg
1342
- else :
1343
- # Lowercase only non-module backend names (modules are case-sensitive)
1344
- arg = arg . lower ()
1345
- name = validate_backend ( arg )
1346
-
1347
- # Check if we've already set up a backend
1348
- if 'matplotlib.backends' in sys . modules :
1349
- # Warn only if called with a different name
1350
- if ( rcParams [ 'backend' ] != name ) and warn :
1351
- import matplotlib . backends
1331
+ name = validate_backend ( arg )
1332
+
1333
+ # if setting back to the same thing, do nothing
1334
+ if ( rcParams [ 'backend' ] == name ) :
1335
+ pass
1336
+
1337
+ # Check if we have already imported pyplot and triggered
1338
+ # backend selection, do a bit more work
1339
+ elif 'matplotlib.pyplot' in sys . modules :
1340
+ # If we are here then the requested is different than the current.
1341
+ # If we are going to force the switch, never warn, else, if warn
1342
+ # is True, then direct users to `plt.switch_backend`
1343
+ if ( not force ) and warn :
1352
1344
warnings .warn (
1353
- _use_error_msg .format (
1354
- backend = rcParams ['backend' ],
1355
- tb = matplotlib .backends ._backend_loading_tb ),
1345
+ ("matplotlib.pyplot as already been imported, "
1346
+ "this call will have no effect." ),
1356
1347
stacklevel = 2 )
1357
1348
1358
- # Unless we've been told to force it, just return
1359
- if not force :
1360
- return
1361
- need_reload = True
1349
+ # if we are going to force switching the backend, pull in
1350
+ # `switch_backend` from pyplot. This will only happen if
1351
+ # pyplot is already imported.
1352
+ if force :
1353
+ from matplotlib .pyplot import switch_backend
1354
+ switch_backend (name )
1355
+ # Finally if pyplot is not imported update both rcParams and
1356
+ # rcDefaults so restoring the defaults later with rcdefaults
1357
+ # won't change the backend. This is a bit of overkill as 'backend'
1358
+ # is already in style.core.STYLE_BLACKLIST, but better to be safe.
1362
1359
else :
1363
- need_reload = False
1364
-
1365
- # Store the backend name
1366
- rcParams ['backend' ] = name
1367
-
1368
- # If needed we reload here because a lot of setup code is triggered on
1369
- # module import. See backends/__init__.py for more detail.
1370
- if need_reload :
1371
- importlib .reload (sys .modules ['matplotlib.backends' ])
1360
+ rcParams ['backend' ] = rcParamsDefault ['backend' ] = name
1372
1361
1373
1362
1374
1363
if os .environ .get ('MPLBACKEND' ):
0 commit comments