question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing yellowbrick changes global matplotlib styles

See original GitHub issue

UPDATE: 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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lwgraycommented, Mar 12, 2019

hi @gavsideas Thanks for adding to the discussion. We are on a very short hiatus and will provide feedback once we return. cheers

0reactions
joelostblomcommented, May 8, 2019

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, neither reset_defaults() or reset_origin() successfully reset the styles to the same as when launching a notebook the first time (tested in JupyterLab only, not classic).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Colors and Style — Yellowbrick v1.5 documentation
This allows you to specify these named colors by the first character, e.g. 'bgrmyck' for matplotlib visualizations. To change the global color palette,...
Read more >
Yellowbrick Documentation - Read the Docs
Once installed, you should be able to import Yellowbrick without ... Arguments that change how the visualization is drawn can be passed into....
Read more >
Yellowbrick change legend and add title - Stack Overflow
You can modify the title of a Yellowbrick plot using the title parameter, and use the size parameter to increase the size of...
Read more >
Yellowbrick Examples - | notebook.community
import numpy as np import pandas as pd import yellowbrick as yb import matplotlib as mpl import matplotlib.pyplot as plt mpl.rcParams["figure.figsize"] = (9 ......
Read more >
yellowbrick/palettes.rst at develop · DistrictDataLabs/yellowbrick ...
import matplotlib.pyplot as plt from yellowbrick.style.palettes import ... To change the global color palette, use the set_palette function as follows:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found