Importing yellowbrick changes global matplotlib styles
See original GitHub issueUPDATE: After some investigation, we’ve discovered that Seaborn has rolled back their automatic style changes on import, and we should probably do the same. To fix this issue the following things need to be done:
- remove the automatic style changes from
yellowbrick/__init__.py
- create documentation similar to Seaborn’s Controlling Image Aesthetics
- determine how we can use our styles on
poof()
without changing the global rcParams
Original Issue
This may be intentional behavior. If it is, sorry for the noise! It surprised me though, so maybe it should be in the documentation somewhere.
When you import yellowbrick
, it runs yellowbrick/__init__.py
which contains this line:
set_aesthetic() # NOTE: modifies mpl.rcParams
This does indeed change matplotlib.rcParams
, which means that every other plot generated via matplotlib
is altered.
I have a workaround, which is to create a context manager for Yellowbrick plots:
import warnings
import matplotlib as mpl
import yellowbrick
class YellowbrickStyle:
"""
Context manager for global mpl style state to accommodate Yellowbrick
"""
def __enter__(self):
yellowbrick.set_aesthetic()
def __exit__(self, *args):
with warnings.catch_warnings():
warnings.simplefilter('ignore', mpl.MatplotlibDeprecationWarning)
yellowbrick.reset_orig()
# Importing yellowbrick modifies global mpl style state so reset it immediately
with YellowbrickStyle():
pass
And then I wrap every use of Yellowbrick in a with YellowbrickStyle():
block, e.g.
with YellowbrickStyle():
visualizer = yellowbrick.regressor.ResidualsPlot(model)
visualizer.fit(X=training_X, y=training_y)
visualizer.score(X=testing_X, y=testing_y)
visualizer.poof()
This works but it’s a bit of a hassle. Did you mean to change the matplotlib rcParams
for every other plot in a project?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top GitHub Comments
hi @gavsideas Thanks for adding to the discussion. We are on a very short hiatus and will provide feedback once we return. cheers
Thanks for looking into this! It would be great if it could be handled the same way as in seaborn, where users have to call
seaborn.set()
to explicitly modify the global matplotlib rc params. In my limited testing, neitherreset_defaults()
orreset_origin()
successfully reset the styles to the same as when launching a notebook the first time (tested in JupyterLab only, not classic).