-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Style RC parameter #4240
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
Closed
Closed
Style RC parameter #4240
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from __future__ import absolute_import | ||
|
||
from .core import use, context, available, library, reload_library | ||
from matplotlib import rcParams | ||
if rcParams['style']: | ||
use(rcParams['style']) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
""" | ||
import os | ||
import re | ||
import sys | ||
import contextlib | ||
import warnings | ||
|
||
|
@@ -33,7 +34,7 @@ | |
USER_LIBRARY_PATHS = [os.path.join(mpl._get_configdir(), 'stylelib')] | ||
STYLE_EXTENSION = 'mplstyle' | ||
STYLE_FILE_PATTERN = re.compile('([\S]+).%s$' % STYLE_EXTENSION) | ||
|
||
PARENT_STYLES = 'style' | ||
|
||
# A list of rcParams that should not be applied from styles | ||
STYLE_BLACKLIST = { | ||
|
@@ -91,26 +92,91 @@ def use(style): | |
""" | ||
if cbook.is_string_like(style) or hasattr(style, 'keys'): | ||
# If name is a single str or dict, make it a single element list. | ||
styles = [style] | ||
else: | ||
styles = style | ||
|
||
for style in styles: | ||
if not cbook.is_string_like(style): | ||
_apply_style(style) | ||
elif style == 'default': | ||
_apply_style(rcParamsDefault, warn=False) | ||
elif style in library: | ||
_apply_style(library[style]) | ||
style = [style] | ||
flattened_style = _flatten_style_dict({PARENT_STYLES: style}) | ||
_apply_style(flattened_style) | ||
|
||
|
||
def _expand_parent(parent_style): | ||
if cbook.is_string_like(parent_style): | ||
if parent_style == "default": | ||
parent_style = rcParamsDefault | ||
else: | ||
try: | ||
rc = rc_params_from_file(style, use_default_template=False) | ||
_apply_style(rc) | ||
except IOError: | ||
msg = ("'%s' not found in the style library and input is " | ||
"not a valid URL or path. See `style.available` for " | ||
"list of available styles.") | ||
raise IOError(msg % style) | ||
parent_style = get_style_dict(parent_style) | ||
return parent_style | ||
|
||
|
||
def flatten_inheritance_dict(child_dict, parent_key, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why leave this public? |
||
expand_parent=lambda x: x): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and why leave this with a default value like this? |
||
"""Return a flattened version of dictionary that inherits from a parent. | ||
|
||
Parameters | ||
---------- | ||
child_dict : dict | ||
Dictionary with a special key that points to a dictionary of defaults, | ||
or a value that can be expanded to a dictionary of defaults. | ||
parent_key : str | ||
The key that points to a list of parents. | ||
expand_parent : callable(parent) -> dict | ||
Function that returns a dictionary from the value corresponding to | ||
`parent_key`. By default, this simply returns the value. | ||
""" | ||
if parent_key not in child_dict: | ||
return child_dict.copy() | ||
|
||
parents = child_dict[parent_key] | ||
if isinstance(parents, dict): | ||
parents = [parents] | ||
if not isinstance(parents, (list, tuple)): | ||
msg = "Parent value must be list or tuple, but given {!r}" | ||
raise ValueError(msg.format(parents)) | ||
|
||
# Expand any parents defined by `child_dict` into dictionaries. | ||
parents = (expand_parent(p) for p in parents) | ||
|
||
# Resolve any grand-parents defined by parents of `child_dict` | ||
parents = [flatten_inheritance_dict(p, parent_key, expand_parent) | ||
for p in parents] | ||
|
||
# Child will override parent values in `dict.update` so put it last. | ||
ordered_dicts = parents + [child_dict] | ||
|
||
# Copy first dictionary and update with subsequent dictionaries. | ||
output_dict = ordered_dicts[0].copy() | ||
for d in ordered_dicts[1:]: | ||
output_dict.update(d) | ||
|
||
# Since the parent data been resolved, remove parent references. | ||
del output_dict[parent_key] | ||
return output_dict | ||
|
||
|
||
def _flatten_style_dict(style_dict): | ||
return flatten_inheritance_dict(style_dict, PARENT_STYLES, | ||
expand_parent=_expand_parent) | ||
|
||
|
||
def get_style_dict(style): | ||
"""Returns a dictionnary containing all the parameters from the | ||
style file. | ||
|
||
Parameters | ||
---------- | ||
style : str | ||
style from the default library, the personal library or any | ||
full path. | ||
""" | ||
if style in library: | ||
return library[style] | ||
else: | ||
try: | ||
return rc_params_from_file(style, | ||
use_default_template=False) | ||
except IOError: | ||
msg = ("'%s' not found in the style library and input is " | ||
"not a valid URL or path. See `style.available` for " | ||
"list of available styles.") | ||
raise IOError(msg % style) | ||
|
||
|
||
@contextlib.contextmanager | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the appropriate place for this call. IIUC, the default style isn't used unless the
style
module is imported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is true, but I was not able to put it anywhere else... I tried to import the style from rc on matplotlib import, but it is not possible to do it from the
mpl.__init__
as the style module uses some functions from the__init__
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a small test, I confirmed something I already noticed before, which is that a style call from the matplotlibrc will be applied even without calling the
matplotlib.style
library. I guess that on init, matplotlib initializes also the style library, or giving the style parameter in the rc will call the style library... I'm not sure about which it is