访问Seaborn set_style()中的图例属性

2024-05-23 14:18:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用sns.set_style()来限制重复设置绘图属性。
我遇到的问题是,我似乎无法找到如何访问我要修改的所有属性。图例颜色

请参见下面的样式表和打印代码:

import seaborn as sns
import numpy as np

dark_plot_no_tick = {
    'axes.facecolor': '#181818',
    'legend.facecolor': "white", # still inheriting from figure
    'axes.edgecolor': '1',
    'axes.grid': False,
    'figure.facecolor': '#181818', # Works inline but not saving
    'xtick.bottom': False, # still visible
    'xtick.top': False,
    'ytick.left': False, # still visible
    'ytick.right': False,
    'axes.spines.left': True,
    'axes.spines.bottom': True,
    'axes.spines.right': False,
    'axes.spines.top': False
}

sns.set_style(rc=dark_plot_no_tick)
sns.set_palette(['#5FB7B2'])

fig, ax = plt.pyplot.subplots()
sns.kdeplot(np.random.normal(10, 10, 10_000), label="test", ax=ax)
fig.savefig("test.png");

唯一的问题是legend facecolor是透明的或继承的,我不知道如何在传递给set_style()的样式表中更改它

下面是它现在的样子:

Incorrectly formatted legend

这就是我想要的样子:

Correctly formatted legend

编辑:
根据@ImportanceOfBeingErnest的建议,我尝试了以下方法:

import matplotlib 

matplotlib.rcParams["legend.facecolor"] = "white"
matplotlib.rcParams["xtick.bottom"] = False
matplotlib.rcParams["ytick.left"] = False

使用rcParams修改legend.facecolor有效,而修改xtick.bottomytick.left无效

也就是说,我仍然希望使用sns.set_style()而不是使用matplotlib.rcParams来修改facecolor

另外,我意识到消除蜱虫不是原始问题的一部分,但我在这里包括了它,因为我相信这两个问题可能是由于相同的根本原因造成的


Tags: importfalsematplotlibstyleleftlegendsetsns