8000 Merge pull request #5440 from mdboom/blacklist-style-params · matplotlib/matplotlib@4c319fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c319fc

Browse files
efiringjenshnielsen
authored andcommitted
Merge pull request #5440 from mdboom/blacklist-style-params
Fix #4855: Blacklist rcParams that aren't style
1 parent 1d89de2 commit 4c319fc

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Style parameter blacklist
2+
-------------------------
3+
4+
In order to prevent unexpected consequences from using a style, style
5+
files are no longer able to set parameters that affect things
6+
unrelated to style. These parameters include::
7+
8+
'interactive', 'backend', 'backend.qt4', 'webagg.port',
9+
'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback',
10+
'toolbar', 'timezone', 'datapath', 'figure.max_open_warning',
11+
'savefig.directory', 'tk.window_focus', 'hardcopy.docstring'

lib/matplotlib/mpl-data/stylelib/classic.mplstyle

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,6 @@ figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray
293293
figure.edgecolor : w # figure edgecolor
294294
figure.autolayout : False # When True, automatically adjust subplot
295295
# parameters to make the plot fit the figure
296-
figure.max_open_warning : 20 # The maximum number of figures to open through
297-
# the pyplot interface before emitting a warning.
298-
# If less than one this feature is disabled.
299296
figure.frameon : True
300297

301298
# The figure subplot parameters. All dimensions are a fraction of the
@@ -400,18 +397,13 @@ savefig.bbox : standard # 'tight' or 'standard'.
400397
# use ffmpeg_file instead
401398
savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'
402399
savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter.
403-
savefig.directory : ~ # default directory in savefig dialog box,
404-
# leave empty to always use current working directory
405400
savefig.transparent : False # setting that controls whether figures are saved with a
406401
# transparent background by default
407402
savefig.frameon : True
408403
savefig.orientation : portrait
409404

410405
nbagg.transparent: True
411406

412-
# tk backend params
413-
tk.window_focus : False # Maintain shell focus for TkAgg
414-
415407
# ps backend params
416408
ps.papersize : letter # auto, letter, legal, ledger, A0-A10, B0-B10
417409
ps.useafm : False # use of afm fonts, results in small files
@@ -482,8 +474,6 @@ keymap.yscale : l # toggle scaling of y-axes ('log'/'linear')
482474
keymap.xscale : k, L # toggle scaling of x-axes ('log'/'linear')
483475
keymap.all_axes : a # enable all axes
484476

485-
toolbar: toolbar2
486-
487477
###ANIMATION settings
488478
animation.writer : ffmpeg # MovieWriter 'backend' to use
489479
animation.codec : mpeg4 # Codec to use for writing movie

lib/matplotlib/style/core.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import os
1919
import re
2020
import contextlib
21+
import warnings
2122

2223
import matplotlib as mpl
2324
from matplotlib import cbook
24-
from matplotlib import rc_params_from_file
25+
from matplotlib import rc_params_from_file, rcParamsDefault
2526

2627

2728
__all__ = ['use', 'context', 'available', 'library', 'reload_library']
@@ -34,11 +35,35 @@
3435
STYLE_FILE_PATTERN = re.compile('([\S]+).%s$' % STYLE_EXTENSION)
3536

3637

38+
# A list of rcParams that should not be applied from styles
39+
STYLE_BLACKLIST = set([
40+
'interactive', 'backend', 'backend.qt4', 'webagg.port',
41+
'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback',
42+
'toolbar', 'timezone', 'datapath', 'figure.max_open_warning',
43+
'savefig.directory', 'tk.window_focus', 'hardcopy.docstring'])
44+
45+
46+
def _remove_blacklisted_style_params(d):
47+
o = {}
48+
for key, val in d.items():
49+
if key in STYLE_BLACKLIST:
50+
warnings.warn(
51+
"Style includes a parameter, '{0}', that is not related to "
52+
"style. Ignoring".format(key))
53+
else:
54+
o[key] = val
55+
return o
56+
57+
3758
def is_style_file(filename):
3859
"""Return True if the filename looks like a style file."""
3960
return STYLE_FILE_PATTERN.match(filename) is not None
4061

4162

63+
def _apply_style(d):
64+
mpl.rcParams.update(_remove_blacklisted_style_params(d))
65+
66+
4267
def use(style):
4368
"""Use matplotlib style settings from a style specification.
4469
@@ -71,18 +96,15 @@ def use(style):
7196

7297
for style in styles:
7398
if not cbook.is_string_like(style):
74-
mpl.rcParams.update(style)
75-
continue
99+
_apply_style(style)
76100
elif style == 'default':
77-
mpl.rcdefaults()
78-
continue
79-
80-
if style in library:
81-
mpl.rcParams.update(library[style])
101+
_apply_style(rcParamsDefault)
102+
elif style in library:
103+
_apply_style(library[style])
82104
else:
83105
try:
84106
rc = rc_params_from_file(style, use_default_template=False)
85-
mpl.rcParams.update(rc)
107+
_apply_style(rc)
86108
except IOError:
87109
msg = ("'%s' not found in the style library and input is "
88110
"not a valid URL or path. See `style.available` for "

0 commit comments

Comments
 (0)
0