Skip to content
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

Avoid changing matplotlib rcParams font size #142

Merged

Conversation

mperrin
Copy link
Collaborator

@mperrin mperrin commented Feb 1, 2024

import spaceKLIP should not have side effects that unexpectedly change the appearance of plots produced by other non-spaceKLIP code. In particular we shouldn't hard-code overrides to a user's default matplotlib settings in rcParams.

@mperrin mperrin added the bug Something isn't working label Feb 1, 2024
@mperrin mperrin self-assigned this Feb 1, 2024
@mperrin
Copy link
Collaborator Author

mperrin commented Feb 1, 2024

Even with this PR, something screwy is still happening that's messing up one of the other plots produced by code in webbpsf, if I import spaceKLIP before running that other function. Very weird and confusing. Continuing to investigate...

@mperrin
Copy link
Collaborator Author

mperrin commented Feb 1, 2024

Found it. The other side effects that were/are messing up my plotting code come from webbpsf_ext.utils which also has hard-coded overwrites of rcParams. I'll follow up on that separately elsewhere.

Attach a context manager decorator to all plotting functions in plotting.py such that they load the spaceKLIP style sheet file. This way it doesn't touch the default matplotlib rcparams dictionary.
add context manager and decorator matplotlib style to various plotting routines around spaceklip.
@JarronL
Copy link
Collaborator

JarronL commented Feb 2, 2024

Okay, so in order to keep things consistent with plotting styles, but not break the default matplotlib rcparams configuration, I have updated both spaceKLIP and webbpsf_ext to utilize matplotlib style sheets. Essentially there is now a file called sk_style.mplstyle in spaceKLIP that is configured for some default set of parameters. This can now get loaded at any time using either plt.style.use('spaceKLIP.sk_style') or the convenience function spaceKLIP.plotting.load_plt_style().

In addition, matplotlib allows for adding context manager decorators to any code to temporarily load this style sheet, so I have gone through all plotting calls in the various .py files and added these decorators to use dynamically use the previously hardcoded rcparams. This means there should be no apparent change in the spaceklip plotting functions, but other packages (such as webbpsf) will not be affected.

If you want to use these context managers, you would simply do the following:

with plt.style.context('spaceKLIP.sk_style'):
    fig, ax = plt.sublots()
    some_plotting_function(ax)
    another_function(ax)
    fig.save()
    plt.close(fig)

Or when defining a function, add a decorator:

@plt.style.context('spaceKLIP.sk_style')
def plotting_function(*args, **kwargs):
    plt.plot(*args, **kwargs)

@JarronL
Copy link
Collaborator

JarronL commented Feb 2, 2024

Also, lots of cool pre-defined matplotlib styles here: https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html

Copy link
Collaborator

@kammerje kammerje left a comment

Choose a reason for hiding this comment

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

This looks like a straight-forward change and I fully agree with it.

@kammerje kammerje merged commit dcce51c into spacetelescope:develop Feb 2, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants