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

Add default theme configuration to Plot #3223

Merged
merged 9 commits into from
Jan 16, 2023
Merged

Add default theme configuration to Plot #3223

merged 9 commits into from
Jan 16, 2023

Conversation

mwaskom
Copy link
Owner

@mwaskom mwaskom commented Jan 15, 2023

This PR introduces a mechanism for changing the appearance of all plots made with so.Plot. It adds a configuration interface, accessible through so.Plot.config (this is an attribute of the class, not an instance). I expect to add a number of configuration objects; this PR adds so.Plot.config.theme, which exposes a dictionary of rcParams that will be used by default in all Plot plots, unless overridden by so.Plot().theme(). So to change the appearance of all plots, you could put at the top of your notebook:

theme = sns.axes_style("whitegrid") | sns.plotting_context("talk")
so.Plot.config.theme.update(theme)

then do

so.Plot()

To use the matplotlib globals (whether determined by a matplotlibrc file or functions that modify the global state, you'd only need do)

so.Plot.config.theme.update(mpl.rcParams)  # or plt.rcParams

I've added a nice _repr_html_ for the theme config object, so you can see the available parameters and what they're set to more easily:

image

(One could even imaging making this an editable table for interactive updating. We'll see...)

Also, we actually store the theme defaults in a matplotlib RcParams object, so we get to take advantage of their validation:

so.Plot.config.theme["axes.linewidth"] = "thick"
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [9], line 1
----> 1 so.Plot.config.theme["axes.linewidth"] = "thick"

File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/__init__.py:651, in RcParams.__setitem__(self, key, val)
    649         cval = self.validate[key](val)
    650     except ValueError as ve:
--> 651         raise ValueError(f"Key {key}: {ve}") from None
    652     dict.__setitem__(self, key, cval)
    653 except KeyError as err:

ValueError: Key axes.linewidth: Could not convert 'thick' to float

One possibility that I'm undecided on is whether there should be more direct access to seaborn's named themes, i.e. something like

so.Plot.config.theme.set(style="ticks", context="paper")

I'm a little unsure of how/where best to document this, but for now I'm adding a configuration section right on the Plot API docs page.

cf. #3005

@codecov
Copy link

codecov bot commented Jan 15, 2023

Codecov Report

Merging #3223 (4d4b591) into master (623b0b7) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 4d4b591 differs from pull request most recent head e0b29f8. Consider uploading reports for the commit e0b29f8 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #3223   +/-   ##
=======================================
  Coverage   98.42%   98.42%           
=======================================
  Files          77       77           
  Lines       24361    24439   +78     
=======================================
+ Hits        23977    24055   +78     
  Misses        384      384           
Impacted Files Coverage Δ
seaborn/_core/plot.py 99.23% <100.00%> (+0.03%) ⬆️
tests/_core/test_plot.py 98.76% <100.00%> (+0.04%) ⬆️

@mwaskom mwaskom merged commit a47b97e into master Jan 16, 2023
@mwaskom mwaskom deleted the plot/default_theme branch January 16, 2023 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant