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

Skip to content

Restore (and warn on) seaborn styles in style.library #24265

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 25, 2022
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
69 changes: 41 additions & 28 deletions lib/matplotlib/style/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ class __getattr__:
'toolbar', 'timezone', 'figure.max_open_warning',
'figure.raise_window', 'savefig.directory', 'tk.window_focus',
'docstring.hardcopy', 'date.epoch'}
_DEPRECATED_SEABORN_STYLES = {
s: s.replace("seaborn", "seaborn-v0_8")
for s in [
"seaborn",
"seaborn-bright",
"seaborn-colorblind",
"seaborn-dark",
"seaborn-darkgrid",
"seaborn-dark-palette",
"seaborn-deep",
"seaborn-muted",
"seaborn-notebook",
"seaborn-paper",
"seaborn-pastel",
"seaborn-poster",
"seaborn-talk",
"seaborn-ticks",
"seaborn-white",
"seaborn-whitegrid",
]
}
_DEPRECATED_SEABORN_MSG = (
"The seaborn styles shipped by Matplotlib are deprecated since %(since)s, "
"as they no longer correspond to the styles shipped by seaborn. However, "
"they will remain available as 'seaborn-v0_8-<style>'. Alternatively, "
"directly use the seaborn API instead.")


def _remove_blacklisted_style_params(d, warn=True):
Expand Down Expand Up @@ -113,31 +139,9 @@ def use(style):
def fix_style(s):
if isinstance(s, str):
s = style_alias.get(s, s)
if s in [
"seaborn",
"seaborn-bright",
"seaborn-colorblind",
"seaborn-dark",
"seaborn-darkgrid",
"seaborn-dark-palette",
"seaborn-deep",
"seaborn-muted",
"seaborn-notebook",
"seaborn-paper",
"seaborn-pastel",
"seaborn-poster",
"seaborn-talk",
"seaborn-ticks",
"seaborn-white",
"seaborn-whitegrid",
]:
_api.warn_deprecated(
"3.6", message="The seaborn styles shipped by Matplotlib "
"are deprecated since %(since)s, as they no longer "
"correspond to the styles shipped by seaborn. However, "
"they will remain available as 'seaborn-v0_8-<style>'. "
"Alternatively, directly use the seaborn API instead.")
s = s.replace("seaborn", "seaborn-v0_8")
if s in _DEPRECATED_SEABORN_STYLES:
_api.warn_deprecated("3.6", message=_DEPRECATED_SEABORN_MSG)
s = _DEPRECATED_SEABORN_STYLES[s]
return s

for style in map(fix_style, styles):
Expand Down Expand Up @@ -244,17 +248,26 @@ def update_nested_dict(main_dict, new_dict):
return main_dict


class _StyleLibrary(dict):
def __getitem__(self, key):
if key in _DEPRECATED_SEABORN_STYLES:
_api.warn_deprecated("3.6", message=_DEPRECATED_SEABORN_MSG)
key = _DEPRECATED_SEABORN_STYLES[key]

return dict.__getitem__(self, key)


# Load style library
# ==================
_base_library = read_style_directory(BASE_LIBRARY_PATH)
library = None
library = _StyleLibrary()
available = []


def reload_library():
"""Reload the style library."""
global library
library = update_user_library(_base_library)
library.clear()
library.update(update_user_library(_base_library))
Copy link
Contributor

Choose a reason for hiding this comment

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

To match previous semantics, library should be cleared first?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch.

available[:] = sorted(library.keys())


Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def test_deprecated_seaborn_styles():
with pytest.warns(mpl._api.MatplotlibDeprecationWarning):
mpl.style.use("seaborn-bright")
assert mpl.rcParams == seaborn_bright
with pytest.warns(mpl._api.MatplotlibDeprecationWarning):
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain this? You just tested this three lines above - do we understand why that test passed?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's style.use, this is style.library.

Copy link
Member

Choose a reason for hiding this comment

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

Ah...

OTOH, do we expect users to access style.library? Should this be private, or is it useful to expose?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know if we expect them to or not, but we already have the bug report from a user, so it is being used. It is also documented, at least a little, in the module docstring. In any case, this is only the bug fix for now.

mpl.style.library["seaborn-bright"]


def test_up_to_date_blacklist():
Expand Down