138
138
139
139
# cbook must import matplotlib only within function
140
140
# definitions, so it is safe to import from it here.
141
- from . import cbook
141
+ from . import cbook , rcsetup
142
142
from matplotlib .cbook import (
143
143
MatplotlibDeprecationWarning , dedent , get_label , sanitize_sequence )
144
144
from matplotlib .cbook import mplDeprecation # deprecated
@@ -853,6 +853,10 @@ def __setitem__(self, key, val):
853
853
cbook .warn_deprecated (
854
854
"3.0" , "{} is deprecated; in the future, examples will be "
855
855
"found relative to the 'datapath' directory." .format (key ))
856
+ elif key == 'backend' :
857
+ if val is rcsetup ._auto_backend_sentinel :
858
+ if 'backend' in self :
859
+ return
856
860
try :
857
861
cval = self .validate [key ](val )
858
862
except ValueError as ve :
@@ -881,6 +885,12 @@ def __getitem__(self, key):
881
885
"3.0" , "{} is deprecated; in the future, examples will be "
882
886
"found relative to the 'datapath' directory." .format (key ))
883
887
888
+ elif key == "backend" :
889
+ val = dict .__getitem__ (self , key )
890
+ if val is rcsetup ._auto_backend_sentinel :
891
+ from matplotlib import pyplot as plt
892
+ plt .switch_backend (rcsetup ._auto_backend_sentinel )
893
+
884
894
return dict .__getitem__ (self , key )
885
895
886
896
def __repr__ (self ):
@@ -1095,10 +1105,10 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
1095
1105
_fullpath = os .path .join (_basedir , rcParams ['examples.directory' ])
1096
1106
rcParams ['examples.directory' ] = _fullpath
1097
1107
1098
- rcParamsOrig = rcParams .copy ()
1099
1108
1100
1109
with warnings .catch_warnings ():
1101
1110
warnings .simplefilter ("ignore" , MatplotlibDeprecationWarning )
1111
+ rcParamsOrig = RcParams (rcParams .copy ())
1102
1112
rcParamsDefault = RcParams ([(key , default ) for key , (default , converter ) in
1103
1113
defaultParams .items ()
1104
1114
if key not in _all_deprecated ])
@@ -1222,7 +1232,7 @@ def rc_file_defaults():
1222
1232
with warnings .catch_warnings ():
1223
1233
warnings .simplefilter ("ignore" , mplDeprecation )
1224
1234
from .style .core import STYLE_BLACKLIST
1225
- rcParams .update ({k : v for k , v in rcParamsOrig . items ()
1235
+ rcParams .update ({k : rcParamsOrig [ k ] for k in rcParamsOrig
1226
1236
if k not in STYLE_BLACKLIST })
1227
1237
1228
1238
@@ -1238,7 +1248,8 @@ def rc_file(fname):
1238
1248
with warnings .catch_warnings ():
1239
1249
warnings .simplefilter ("ignore" , mplDeprecation )
1240
1250
from .style .core import STYLE_BLACKLIST
1241
- rcParams .update ({k : v for k , v in rc_params_from_file (fname ).items ()
1251
+ rc_from_file = rc_params_from_file (fname )
1252
+ rcParams .update ({k : rc_from_file [k ] for k in rc_from_file
1242
1253
if k not in STYLE_BLACKLIST })
1243
1254
1244
1255
@@ -1289,16 +1300,23 @@ def __init__(self, rc=None, fname=None):
1289
1300
if rc :
1290
1301
rcParams .update (rc )
1291
1302
except Exception :
1292
- # If anything goes wrong, revert to the original rcs.
1293
- dict .update (rcParams , self ._orig )
1303
+ self .__fallback ()
1294
1304
raise
1295
1305
1306
+ def __fallback (self ):
1307
+ # If anything goes wrong, revert to the original rcs.
1308
+ updated_backend = self ._orig ['backend' ]
1309
+ dict .update (rcParams , self ._orig )
1310
+ # except for the backend. If the context block triggered resloving
1311
+ # the auto backend resolution keep that value around
1312
+ if self ._orig ['backend' ] is rcsetup ._auto_backend_sentinel :
1313
+ rcParams ['backend' ] = updated_backend
1314
+
1296
1315
def __enter__ (self ):
1297
1316
return self
1298
1317
1299
1318
def __exit__ (self , exc_type , exc_value , exc_tb ):
1300
- # No need to revalidate the original values.
1301
- dict .update (rcParams , self ._orig )
1319
+ self .__fallback ()
1302
1320
1303
1321
1304
1322
def use (arg , warn = True , force = False ):
@@ -1324,14 +1342,14 @@ def use(arg, warn=True, force=False):
1324
1342
1325
1343
force : bool, optional
1326
1344
If True, attempt to switch the backend. This defaults to
1327
- false and using `.pyplot.switch_backend` is preferred .
1345
+ False .
1328
1346
1329
1347
1330
1348
"""
1331
1349
name = validate_backend (arg )
1332
1350
1333
1351
# if setting back to the same thing, do nothing
1334
- if (rcParams [ 'backend' ] == name ):
1352
+ if (dict . __getitem__ ( rcParams , 'backend' ) == name ):
1335
1353
pass
1336
1354
1337
1355
# Check if we have already imported pyplot and triggered
@@ -1361,7 +1379,7 @@ def use(arg, warn=True, force=False):
1361
1379
1362
1380
1363
1381
if os .environ .get ('MPLBACKEND' ):
1364
- use ( os .environ [ 'MPLBACKEND' ] )
1382
+ rcParams [ 'backend' ] = os .environ . get ( 'MPLBACKEND' )
1365
1383
1366
1384
1367
1385
def get_backend ():
0 commit comments