我已经找到了一个明确的答案并且找不到一个,如果之前有人问过,我道歉.我正在使用seaborn 0.6和matplotlib 1.4.3.我想在ipython笔记本中创建许多数字时暂时改变绘图的样式.
具体来说,在这个例子中,我想基于每个图更改字体大小和背景样式.
这创建了我正在寻找的图,但是全局定义了参数:
import seaborn as sns
import numpy as np
x = np.random.normal(size=100)
sns.set(style="whitegrid", font_scale=1.5)
sns.kdeplot(x, shade=True);
Run Code Online (Sandbox Code Playgroud)
然而这失败了:
with sns.set(style="whitegrid", font_scale=1.5):
sns.kdeplot(x, shade=True);
Run Code Online (Sandbox Code Playgroud)
有:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-70c5b03f9aa8> in <module>()
----> 1 with sns.set(style="whitegrid", font_scale=1.5):
2 sns.kdeplot(x, shade=True);
AttributeError: __exit__
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
with sns.axes_style(style="whitegrid", rc={'font.size':10}):
sns.kdeplot(x, shade=True);
Run Code Online (Sandbox Code Playgroud)
哪个不会失败,但它也不会改变字体的大小.任何帮助将非常感激.
mwa*_*kom 10
最好的办法是将seaborn样式和上下文参数组合到一个字典中,然后将其传递给plt.rc_context函数:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
x = np.random.normal(size=100)
with plt.rc_context(dict(sns.axes_style("whitegrid"),
**sns.plotting_context("notebook", font_scale=1.5))):
sns.kdeplot(x, shade=True)
Run Code Online (Sandbox Code Playgroud)