Skip to content

Fix xkcd() not resetting context anymore. #9603

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

Merged
merged 1 commit into from
Oct 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import six

import sys
import warnings
import time
import types
import warnings

from cycler import cycler
import matplotlib
Expand Down Expand Up @@ -389,28 +389,38 @@ def xkcd(scale=1, length=100, randomness=2):
"xkcd mode is not compatible with text.usetex = True")

from matplotlib import patheffects
context = rc_context()
try:
rcParams['font.family'] = ['xkcd', 'Humor Sans', 'Comic Sans MS']
rcParams['font.size'] = 14.0
rcParams['path.sketch'] = (scale, length, randomness)
rcParams['path.effects'] = [
patheffects.withStroke(linewidth=4, foreground="w")]
rcParams['axes.linewidth'] = 1.5
rcParams['lines.linewidth'] = 2.0
rcParams['figure.facecolor'] = 'white'
rcParams['grid.linewidth'] = 0.0
rcParams['axes.grid'] = False
rcParams['axes.unicode_minus'] = False
rcParams['axes.edgecolor'] = 'black'
rcParams['xtick.major.size'] = 8
rcParams['xtick.major.width'] = 3
rcParams['ytick.major.size'] = 8
rcParams['ytick.major.width'] = 3
except:
context.__exit__(*sys.exc_info())
raise
return context
xkcd_ctx = rc_context({
'font.family': ['xkcd', 'Humor Sans', 'Comic Sans MS'],
'font.size': 14.0,
'path.sketch': (scale, length, randomness),
'path.effects': [patheffects.withStroke(linewidth=4, foreground="w")],
'axes.linewidth': 1.5,
'lines.linewidth': 2.0,
'figure.facecolor': 'white',
'grid.linewidth': 0.0,
'axes.grid': False,
'axes.unicode_minus': False,
'axes.edgecolor': 'black',
'xtick.major.size': 8,
'xtick.major.width': 3,
'ytick.major.size': 8,
'ytick.major.width': 3,
})
xkcd_ctx.__enter__()

# In order to make the call to `xkcd` that does not use a context manager
# (cm) work, we need to enter into the cm ourselves, and return a dummy
# cm that does nothing on entry and cleans up the xkcd context on exit.
# Additionally, we need to keep a reference to the dummy cm because it
# would otherwise be exited when GC'd.

class dummy_ctx(object):
def __enter__(self):
pass

__exit__ = xkcd_ctx.__exit__

return dummy_ctx()


## Figures ##
Expand Down
15 changes: 14 additions & 1 deletion lib/matplotlib/tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest

import matplotlib as mpl
from matplotlib import style
from matplotlib import pyplot as plt, style
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, is this import style "ok"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, doing import sys, os is what is discouraged.

In that case that you are importing mulitple top level things is obscured, in this case we are importing multiple things from the same namespace (and renaming one). https://www.python.org/dev/peps/pep-0008/#imports

from matplotlib.style.core import USER_LIBRARY_PATHS, STYLE_EXTENSION

import six
Expand Down Expand Up @@ -158,3 +158,16 @@ def test_alias(equiv_styles):
rc_base = rc_dicts[0]
for nm, rc in zip(equiv_styles[1:], rc_dicts[1:]):
assert rc_base == rc


def test_xkcd_no_cm():
assert mpl.rcParams["path.sketch"] is None
plt.xkcd()
assert mpl.rcParams["path.sketch"] == (1, 100, 2)


def test_xkcd_cm():
assert mpl.rcParams["path.sketch"] is None
with plt.xkcd():
assert mpl.rcParams["path.sketch"] == (1, 100, 2)
assert mpl.rcParams["path.sketch"] is None