导入 seaborn 停止 set rc_params 工作

importing seaborn stops set rc_params from working

当我运行下面的代码时,如果注释掉导入 seaborn 的行,它工作得很好,可以在函数中设置字体大小,并在整个绘图中设置它(我将它用于具有多个子图和轴的更复杂的功能,并且需要通用字体设置)。为什么 seaborn 会阻止我的 with plt.rc_context({'font.size': fontsize,}): 工作,我怎样才能阻止它这样做同时仍然能够使用 seaborn 的功能? (我不需要它的样式默认值,但如果解决方案涉及删除它们)

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")

我通过添加

解决了这个问题
# reset RC params to original
sns.reset_orig()

在我导入 seaborn 以撤消它后,它对 matplotlib 的 rc 参数进行了更改

如果你不想让seaborn做任何样式改变,你可以单独导入seaborn API:

import seaborn.apionly as sns

这在问题的情况下也能正常工作。