修改所有子图的字体大小使用sns.set和rcParams。

huangapple go评论72阅读模式
英文:

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:

修改所有子图的字体大小使用sns.set和rcParams。
修改所有子图的字体大小使用sns.set和rcParams。

Why? How can I fix this?

答案1

得分: 2

  • 在处理 sns.setsns.set_stylercParams 的许多答案中。
  • 如在 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.2matplotlib 3.7.1seaborn 0.12.2 中测试过

修改所有子图的字体大小使用sns.set和rcParams。

修改所有子图的字体大小使用sns.set和rcParams。

英文:
  • There are many answers dealing with sns.set, sns.set_style, and rcParams.
  • As shown in the seaborn rcmod.py file, rcParms are already being set by the seaborn functions.
  • .set is an alias for .set_theme
  • Override rcParams set by .set_theme using the parameters as defined here.
  • In the case of this code, remove sns.set(style="darkgrid") and plt.rcParams['font.size'] = size, and use sns.set_theme(context={'font.size': size}) instead.
    • Using rcParams directly, when using sns.set_theme, does not seem to work.
  • 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)

修改所有子图的字体大小使用sns.set和rcParams。

修改所有子图的字体大小使用sns.set和rcParams。

huangapple
  • 本文由 发表于 2023年5月18日 01:46:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定