Skip to content

Rework style loading - #164

Open
alexfikl wants to merge 5 commits into
garrettj403:masterfrom
alexfikl:fix-mpl-311
Open

Rework style loading#164
alexfikl wants to merge 5 commits into
garrettj403:masterfrom
alexfikl:fix-mpl-311

Conversation

@alexfikl

@alexfikl alexfikl commented Jun 20, 2026

Copy link
Copy Markdown

This implements the changes discussed in #163. Namely,

  1. Reworks read_styles_in_folders to just use importlib.resources to find the styles and make a dict of them.
  2. Adds a manual StyleRegistry thing that users can call to load style using
plt.style.use(scienceplots.styles(["science", "ieee"]))
# or
with plt.style.context(scienceplots.styles(["science", "ieee"])):
    # do things..
  1. Updates the tests, examples and README to reference this new style.
  2. With 1+2, scienceplots essentially no longer depends on matplotlib directly!

As expected, this is completely breaking, since it no longer automatically loads the styles and adds them to matplotlib's registry. Unfortunately, the public style API doesn't offer any way to "register" new styles by name, so this seems to be the most "future proof" option.

Fixes #163

Comment on lines +8 to +16
def get(self, names):
if isinstance(names, str):
names = [names]

# Update dictionary of styles - plt.style.library
plt.style.core.update_nested_dict(plt.style.library, stylesheets)
# Update `plt.style.available`, copy-paste from:
# https://github.com/matplotlib/matplotlib/blob/a170539a421623bb2967a45a24bb7926e2feb542/lib/matplotlib/style/core.py#L266 # noqa: E501
plt.style.core.available[:] = sorted(plt.style.library.keys())
for name in names:
if name not in self.stylesheets:
raise ValueError(f"'{name}' is not a known SciencePlots style")

return [self.stylesheets[name] for name in names]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This currently throws a hard error on unrecognized styles, so we can't do something like

plt.style.use(scienceplots.styles(["dark_background", "science"]))

# register the bundled stylesheets in the matplotlib style library
scienceplots_path = scienceplots.__path__[0]
styles_path = os.path.join(scienceplots_path, "styles")
class StyleRegistry:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This class is intentionally pretty empty. We can try to mirror some of the matplotlib API to shorten some calls, i.e. add some

scienceplots.styles.use([...])
scienceplots.styles.context([...])

Personally, I set the styles at the start of my scripts, so it doesn't matter much if it's a bit more verbose.

@alexfikl
alexfikl marked this pull request as ready for review June 20, 2026 11:46
@alexfikl

Copy link
Copy Markdown
Author

scienceplots uses Python >=3.8. I think most of the new things are supported on that version (from reading the docs), but I haven't checked.

@echedey-ls

Copy link
Copy Markdown
Collaborator

Thanks for the PR. May I know if any of it was generated with AI? It doesn't look like it but I prefer to know it so I can focus on what must be double-checked. In any case, I may pursue a more opinionated path.

@alexfikl

Copy link
Copy Markdown
Author

Thanks for the PR. May I know if any of it was generated with AI? It doesn't look like it but I prefer to know it so I can focus on what must be double-checked. In any case, I may pursue a more opinionated path.

Nope, no AI, but it was only tested on Arch with matplotlib 3.11 and Python 3.14, so it would definitely need some careful checks on that side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Breaking changes in matplotlib 3.11

2 participants