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

Skip to content

Commit b517969

Browse files
committed
Add xkcd style.
1 parent 6a89fda commit b517969

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
xkcd style
2+
``````````
3+
4+
The xkcd style previously available as `.pyplot.xkcd` is now also available as
5+
a regular style file, which can be used as ``matplotlib.style.use("xkcd")``.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from matplotlib import patheffects
2+
3+
4+
__mpl_style__ = {
5+
"axes.edgecolor": "black",
6+
"axes.grid": False,
7+
"axes.linewidth": 1.5,
8+
"axes.unicode_minus": False,
9+
"figure.facecolor": "white",
10+
"font.family": ["xkcd", "Humor Sans", "Comic Sans MS"],
11+
"font.size": 14.0,
12+
"grid.linewidth": 0.0,
13+
"lines.linewidth": 2.0,
14+
"path.effects": [patheffects.withStroke(linewidth=4, foreground="w")],
15+
"path.sketch": (1, 100, 2),
16+
"text.usetex": False,
17+
"xtick.major.size": 8,
18+
"xtick.major.width": 3,
19+
"ytick.major.size": 8,
20+
"ytick.major.width": 3,
21+
}

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -399,24 +399,9 @@ def xkcd(scale=1, length=100, randomness=2):
399399
if rcParams['text.usetex']:
400400
raise RuntimeError(
401401
"xkcd mode is not compatible with text.usetex = True")
402-
403-
from matplotlib import patheffects
404402
return rc_context({
405-
'font.family': ['xkcd', 'Humor Sans', 'Comic Sans MS'],
406-
'font.size': 14.0,
403+
**style.library["xkcd"],
407404
'path.sketch': (scale, length, randomness),
408-
'path.effects': [patheffects.withStroke(linewidth=4, foreground="w")],
409-
'axes.linewidth': 1.5,
410-
'lines.linewidth': 2.0,
411-
'figure.facecolor': 'white',
412-
'grid.linewidth': 0.0,
413-
'axes.grid': False,
414-
'axes.unicode_minus': False,
415-
'axes.edgecolor': 'black',
416-
'xtick.major.size': 8,
417-
'xtick.major.width': 3,
418-
'ytick.major.size': 8,
419-
'ytick.major.width': 3,
420405
})
421406

422407

lib/matplotlib/style/core.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
"""
1313

1414
import contextlib
15+
import importlib
1516
import logging
1617
import os
18+
from pathlib import Path
1719
import re
1820
import warnings
1921

@@ -148,8 +150,10 @@ def context(style, after_reset=False):
148150

149151
def load_base_library():
150152
"""Load style library defined in this package."""
151-
library = read_style_directory(BASE_LIBRARY_PATH)
152-
return library
153+
return {
154+
**read_style_directory(BASE_LIBRARY_PATH),
155+
**_read_pystyle_base_directory(),
156+
}
153157

154158

155159
def iter_user_libraries():
@@ -192,6 +196,27 @@ def read_style_directory(style_dir):
192196
return styles
193197

194198

199+
def _read_pystyle_base_directory():
200+
"""
201+
Return directory of styles defined in *style_dir* as Python files.
202+
203+
Because Python style files can execute arbitrary code, this feature is only
204+
used internally to load files provided by Matplotlib itself, not
205+
user-provided style files.
206+
207+
A Python style is a Python module that exports a dict named
208+
``__mpl_style__``, from which a `RcParams` is constructed. This convention
209+
may be revisited if this feature ever becomes public.
210+
"""
211+
styles = {}
212+
for path in Path(BASE_LIBRARY_PATH).glob("*.py"):
213+
spec = importlib.util.spec_from_file_location(path.stem, path)
214+
module = importlib.util.module_from_spec(spec)
215+
spec.loader.exec_module(module)
216+
styles[path.stem] = mpl.RcParams(module.__mpl_style__)
217+
return styles
218+
219+
195220
def update_nested_dict(main_dict, new_dict):
196221
"""Update nested dict (only level of nesting) with new values.
197222

0 commit comments

Comments
 (0)