4

I have some predefined matplotlibrc style which I use for my plots. Below is a sample image styled with it

enter image description here

On the other hand, I find seaborn's sns.set(font_scale=1.25) quite handy, which allows me to quickly control font sizes.
Yet, while changing the font size it also applies the default seaborn style to my plot, so matplotlib's defaults are overwritten.
I tried sns.set(style=None, font_scale=1.25) instead but line colors and font family of axis labels are still changed.

enter image description here

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

#sns.set(style=None, font_scale=1.25)

fig = plt.figure(figsize=(3.4, 2.1), frameon=False)
fig.tight_layout()

x = np.linspace(0, 2, 500)

ax = fig.add_subplot(111)
ax.set_xlabel('xlabel, some units')
ax.set_ylabel('ylabel, some units')

ax.plot(x, x**0.5, label='$x^{0.5}$')
ax.plot(x, x**1.5, label='$x^{1.5}$')
ax.legend()

fig.savefig('output.png')
plt.close(fig)

2 Answers 2

4

You are looking for set_context:

sns.set_context("notebook", font_scale=1.25)

This will scale the fonts with respect to the predefined "notebook" style, which seems closest to the matplotlib defaults.

A comparisson:

Default plot:

enter image description here

With sns.set_context(font_scale=1.25):

enter image description here

With sns.set_context("notebook", font_scale=1.25):

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

Well, it looks context="notebook" is the default of sns.set, so it doesn't change the style.
Yep exactly, as I understood you do not want to change the style, just the font sizes.
I mean setting "notebook" context is the same as sns.set(font_scale=1.25), and it does change a few things.
It looks, for the second plot context=None (I was getting similar output for this setting). What is your seaborn version?
I have the newest seaborn version (0.8.1) installed.
|
2

One possibility is to rite your own function that reproduces what seaborn is doing "under the hood"

This is adapted from seaborn's code on github:

def scale_fonts(font_scale):
    font_keys = ["axes.labelsize", "axes.titlesize", "legend.fontsize",
             "xtick.labelsize", "ytick.labelsize", "font.size"]
    font_dict = {k: matplotlib.rcParams[k] * font_scale for k in font_keys}
    matplotlib.rcParams.update(font_dict)

You have to make sure the values for the font_keys above are in numeric (e.g. 12 and not "medium") in your rc-file, but otherwise, that's all there is to it.

1 Comment

Thanks! I just started considering setting font_keys (except font.size) with non-numeric values, so changing font.size in rcParams will affect font_keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.