kammerje / spaceKLIP

Pipeline for reducing JWST high-contrast imaging data. Published in Kammerer et al. 2022 and Carter et al. 2022.
https://ui.adsabs.harvard.edu/abs/2022SPIE12180E..3NK/abstract
MIT License
16 stars 9 forks source link

Avoid changing matplotlib rcParams font size #142

Closed mperrin closed 6 months ago

mperrin commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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.

JarronL commented 6 months ago

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 commented 6 months ago

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