SomeRandomPhysicist
SomeRandomPhysicist

Reputation: 1599

importing seaborn stops set rc_params from working

When I run the below code it works perfectly fine if the line importing seaborn is commented out, one can set the fontsize in the function and it sets it throughout the plot (I am using it for a more complicated function with several subplots and axes and want a universal font setting). Why is seaborn stopping my with plt.rc_context({'font.size': fontsize,}): from working and how might I stop it doing as such while still being able to make use of seaborn's functionality? (I don't need it's styling defaults though if the solution involves removing those)

import matplotlib.pyplot as plt
import numpy as np
import seaborn

def plotthing(x, y, fontsize=8):
    with plt.rc_context({'font.size': fontsize,}):
        fig, ax = plt.subplots()
        ax.plot(x, y)
        ax.set_xlabel("x")
        ax.set_xlabel("y")
    return fig, ax

x = np.arange(0, 10)
y = 2*x**2

fig, ax = plotthing(x, y, fontsize=2)
fig.savefig("test.pdf")

Upvotes: 1

Views: 304

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339250

If you don't want seaborn to make any style changes, you may import the seaborn API alone:

import seaborn.apionly as sns

This also works fine in the case from the question.

Upvotes: 1

SomeRandomPhysicist
SomeRandomPhysicist

Reputation: 1599

I solved this by adding

# reset RC params to original
sns.reset_orig()

After I imported seaborn to undo it's changes to matplotlib's rc params

Upvotes: 2

Related Questions