@@ -408,12 +408,11 @@ def setp(obj, *args, **kwargs):
408
408
409
409
def xkcd (scale = 1 , length = 100 , randomness = 2 ):
410
410
"""
411
- Turn on `xkcd <https://xkcd.com/>`_ sketch-style drawing mode.
412
- This will only have effect on things drawn after this function is
413
- called.
411
+ Turn on `xkcd <https://xkcd.com/>`_ sketch-style drawing mode. This will
412
+ only have effect on things drawn after this function is called.
414
413
415
414
For best results, the "Humor Sans" font should be installed: it is
416
- not included with matplotlib .
415
+ not included with Matplotlib .
417
416
418
417
Parameters
419
418
----------
@@ -440,29 +439,46 @@ def xkcd(scale=1, length=100, randomness=2):
440
439
# This figure will be in regular style
441
440
fig2 = plt.figure()
442
441
"""
443
- if rcParams ['text.usetex' ]:
444
- raise RuntimeError (
445
- "xkcd mode is not compatible with text.usetex = True" )
446
-
447
- from matplotlib import patheffects
448
- return rc_context ({
449
- 'font.family' : ['xkcd' , 'xkcd Script' , 'Humor Sans' , 'Comic Neue' ,
450
- 'Comic Sans MS' ],
451
- 'font.size' : 14.0 ,
452
- 'path.sketch' : (scale , length , randomness ),
453
- 'path.effects' : [patheffects .withStroke (linewidth = 4 , foreground = "w" )],
454
- 'axes.linewidth' : 1.5 ,
455
- 'lines.linewidth' : 2.0 ,
456
- 'figure.facecolor' : 'white' ,
457
- 'grid.linewidth' : 0.0 ,
458
- 'axes.grid' : False ,
459
- 'axes.unicode_minus' : False ,
460
- 'axes.edgecolor' : 'black' ,
461
- 'xtick.major.size' : 8 ,
462
- 'xtick.major.width' : 3 ,
463
- 'ytick.major.size' : 8 ,
464
- 'ytick.major.width' : 3 ,
465
- })
442
+ return _xkcd (scale , length , randomness )
443
+
444
+
445
+ class _xkcd :
446
+ # This cannot be implemented in terms of rc_context() because this needs to
447
+ # work as a non-contextmanager too.
448
+
449
+ def __init__ (self , scale , length , randomness ):
450
+ self ._orig = rcParams .copy ()
451
+
452
+ if rcParams ['text.usetex' ]:
453
+ raise RuntimeError (
454
+ "xkcd mode is not compatible with text.usetex = True" )
455
+
456
+ from matplotlib import patheffects
457
+ rcParams .update ({
458
+ 'font.family' : ['xkcd' , 'xkcd Script' , 'Humor Sans' , 'Comic Neue' ,
459
+ 'Comic Sans MS' ],
460
+ 'font.size' : 14.0 ,
461
+ 'path.sketch' : (scale , length , randomness ),
462
+ 'path.effects' : [
463
+ patheffects .withStroke (linewidth = 4 , foreground = "w" )],
464
+ 'axes.linewidth' : 1.5 ,
465
+ 'lines.linewidth' : 2.0 ,
466
+ 'figure.facecolor' : 'white' ,
467
+ 'grid.linewidth' : 0.0 ,
468
+ 'axes.grid' : False ,
469
+ 'axes.unicode_minus' : False ,
470
+ 'axes.edgecolor' : 'black' ,
471
+ 'xtick.major.size' : 8 ,
472
+ 'xtick.major.width' : 3 ,
473
+ 'ytick.major.size' : 8 ,
474
+ 'ytick.major.width' : 3 ,
475
+ })
476
+
477
+ def __enter__ (self ):
478
+ return self
479
+
480
+ def __exit__ (self , * args ):
481
+ dict .update (rcParams , self ._orig )
466
482
467
483
468
484
## Figures ##
0 commit comments