英文:
How to change the font.size for all subplots with sns.set and rcParams
问题
I'm trying to change all font elements in a series of boxplots I'm plotting. My code is similar to
from pathlib import Path
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
def plot_group(df, nrow, ncol, size, plot_title):
sns.set(style="darkgrid")
plt.rcParams['font.size'] = size
fig, axes = plt.subplots(nrow, ncol, figsize=(15, 10), tight_layout=True)
fig.suptitle(plot_title)
n_boxplots = 2
for i in range(nrow):
for j in range(ncol):
current_ax = axes[i, j]
sns.boxplot(data=df[['foo','bar']], palette="Set2", ax=current_ax)
current_ax.set_title("foo", fontsize=18)
plt.savefig(f'font_size_{size}.png', dpi=75)
plt.close(fig)
nrow = 3
ncol = 3
rng = np.random.default_rng(0)
arr = rng.random((30, 2))
df = pd.DataFrame(data=arr, columns=['foo', 'bar'])
for size in [14, 22]:
plot_title = f"Font size = {size}"
plot_group(df, nrow, ncol, size, plot_title)
However, this results in two figures where only the suptitle font size changes, but everything else stays the same. Why? How can I fix this?
英文:
I'm trying to change all font elements in a series of boxplots I'm plotting. My code is similar to
from pathlib import Path
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
def plot_group(df, nrow, ncol, size, plot_title):
sns.set(style="darkgrid")
plt.rcParams['font.size'] = size
fig, axes = plt.subplots(nrow, ncol, figsize=(15, 10), tight_layout=True)
fig.suptitle(plot_title)
n_boxplots = 2
for i in range(nrow):
for j in range(ncol):
current_ax = axes[i, j]
sns.boxplot(data=df[['foo','bar']], palette="Set2", ax=current_ax)
current_ax.set_title("foo", fontsize=18)
plt.savefig(f'font_size_{size}.png', dpi=75)
plt.close(fig)
nrow = 3
ncol = 3
rng = np.random.default_rng(0)
arr = rng.random((30, 2))
df = pd.DataFrame(data=arr, columns=['foo', 'bar'])
for size in [14, 22]:
plot_title = f"Font size = {size}"
plot_group(df, nrow, ncol, size, plot_title)
However, this results in two figures where only the suptitle font size changes, but everything else stays the same:
Why? How can I fix this?
答案1
得分: 2
- 在处理
sns.set、sns.set_style和rcParams的许多答案中。 - 如在 seaborn rcmod.py 文件中所示,
rcParms已经被 seaborn 函数设置。 .set是.set_theme的别名。- 使用 此处 定义的参数覆盖
.set_theme设置的rcParams。 - 有
_context_keys和_style_keys。 - 在这段代码中,移除
sns.set(style="darkgrid")和plt.rcParams['font.size'] = size,改用sns.set_theme(context={'font.size': size})。 - 使用
sns.set_theme时,直接使用rcParams似乎无效。 - 在
python 3.11.2、matplotlib 3.7.1、seaborn 0.12.2中测试过
英文:
- There are many answers dealing with
sns.set,sns.set_style, andrcParams. - As shown in the seaborn rcmod.py file,
rcParmsare already being set by the seaborn functions. .setis an alias for.set_theme- Override
rcParamsset by.set_themeusing the parameters as defined here.- There are
_context_keysand_style_keys.
- There are
- In the case of this code, remove
sns.set(style="darkgrid")andplt.rcParams['font.size'] = size, and usesns.set_theme(context={'font.size': size})instead.- Using
rcParamsdirectly, when usingsns.set_theme, does not seem to work.
- Using
- Tested in
python 3.11.2,matplotlib 3.7.1,seaborn 0.12.2
def plot_group(df, nrow, ncol, size, plot_title):
sns.set_theme(context={'font.size': size}) # use set_theme
fig, axes = plt.subplots(nrow, ncol, figsize=(15, 10))
axes = axes.flat
fig.suptitle(plot_title)
for ax in axes:
sns.boxplot(data=df, palette="Set2", ax=ax)
ax.set_title("foo", fontsize=18)
plt.savefig(f'font_size_{size}.png', dpi=75)
plt.close(fig)
nrow = 3
ncol = 3
rng = np.random.default_rng(0)
arr = rng.random((30, 2))
df = pd.DataFrame(data=arr, columns=['foo', 'bar'])
for size in [14, 22]:
plot_title = f"Font size = {size}"
plot_group(df, nrow, ncol, size, plot_title)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。






评论