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.

Use seaborn with custom matplotlib styles

See original GitHub issue

Stackoverflow answers here and here, and the docsting here (and I assume many other places) incorrectly point out that using import seaborn.apionly as sns or sns.reset_orig() can prevent seaborn from rewriting custom rc parameters. In truth, this seems to just revert to the matplotlib defaults.

Ideally I would like to get the results provided by the following code, without having to insert a call to my custom parameters in between the seaborn import and the plot call

import matplotlib.pyplot as plt

import seaborn as sns
sns.set(style="ticks")
plt.style.use(u'ggplot')

# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")

# Show the results of a linear regression within each dataset
sns.lmplot(x="x", y="y", col="dataset", hue="dataset", data=df,
           col_wrap=2, ci=None, palette="muted", size=4,
           scatter_kws={"s": 50, "alpha": 1})
plt.show()

None of the following alternatives produce a comparable result:

I.

import matplotlib.pyplot as plt

plt.style.use(u'ggplot')
import seaborn.apionly as sns
sns.set(style="ticks")

# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")

# Show the results of a linear regression within each dataset
sns.lmplot(x="x", y="y", col="dataset", hue="dataset", data=df,
           col_wrap=2, ci=None, palette="muted", size=4,
           scatter_kws={"s": 50, "alpha": 1})
plt.show()

II.

import matplotlib.pyplot as plt

plt.style.use(u'ggplot')
import seaborn as sns
sns.reset_orig()
sns.set(style="ticks")

# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")

# Show the results of a linear regression within each dataset
sns.lmplot(x="x", y="y", col="dataset", hue="dataset", data=df,
           col_wrap=2, ci=None, palette="muted", size=4,
           scatter_kws={"s": 50, "alpha": 1})
plt.show()

III.

import matplotlib.pyplot as plt

import seaborn as sns
sns.set(style="ticks")

# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")

# Show the results of a linear regression within each dataset
sns.lmplot(x="x", y="y", col="dataset", hue="dataset", data=df,
           col_wrap=2, ci=None, palette="muted", size=4,
           scatter_kws={"s": 50, "alpha": 1})
plt.style.use(u'ggplot')
plt.show()

IV.

import matplotlib.pyplot as plt

import seaborn.apionly as sns
sns.set(style="ticks")

# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")

# Show the results of a linear regression within each dataset
sns.lmplot(x="x", y="y", col="dataset", hue="dataset", data=df,
           col_wrap=2, ci=None, palette="muted", size=4,
           scatter_kws={"s": 50, "alpha": 1})
plt.style.use(u'ggplot')
plt.show()

Can you help me out? I write wrappers for my plotting functions which I call from PythonTeX or other analysis scripts. To continue using the initial code sequence I would need to mess up my API for all functions that somewhere use any module that imports seaborn, and pass them matplotlibrc as an argument all the way down.

It would be really great if seaborn had the option to play nice and not touch matplotlibrc.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TheChymeracommented, Dec 12, 2016

@mmagnuski thank you so much for the pointer. Indeed, the version bump fixed this. As a positive side-effect of my run-in with this bug, I have now added =dev-python/seaborn-0.7.1 to the Gentoo package tree.

0reactions
mmagnuskicommented, Dec 4, 2016

@TheChymera Yes, I was able to replicate your problems on 0.7.0 but not after upgrading to 0.7.1. One of the things that changed in 0.7.1 is that seaborn saves the matplotlib rc params at import and in apionly applies them back in the end.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Controlling figure aesthetics — seaborn 0.12.1 documentation
Matplotlib is highly customizable, but it can be hard to know what settings to ... To control the style, use the axes_style() and...
Read more >
How To Create And Use Custom Matplotlib Style Sheet
Here we penguin dataset as an example to demonstrate the effect of the custom style sheet signaure.mplstyle . import matplotlib.pyplot as plt import...
Read more >
Customizing Matplotlib with style sheets and rcParams
You can create custom styles and use them by calling style.use with the path or URL to the style sheet. ... Alternatively, you...
Read more >
Customizing Matplotlib: Configurations and Stylesheets
import matplotlib.pyplot as plt plt.style.use('classic') import numpy as np ... That said, I prefer to customize Matplotlib using its stylesheets instead.
Read more >
Seaborn Styling, Part 1: Figure Style and Scale - Codecademy
Seaborn has five built-in themes to style its plots: darkgrid , whitegrid , dark , white , and ticks . Seaborn defaults to...
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