Seaborn 配置隐藏了默认的 matplotlib
Seaborn configuration hides default matplotlib
Seaborn 提供了一些图形,这些图形对于科学数据表示非常有趣。
因此,我开始将这些 Seaborn 图形与其他定制的 matplotlib 图穿插使用。
问题是一旦我这样做了:
import seaborn as sb
这个导入似乎全局设置了seaborn的图形参数,然后导入下面的所有matplotlib图形都得到了seaborn参数(它们得到了灰色背景,linewithd变化等等)。
SO 中有 an answer 解释了如何使用 matplotlib 配置生成 seaborn 图,但我想要的是在同时使用两个库时保持 matplotlib 配置参数不变,同时能够生成,在需要时,原始的 seaborn 地块。
如果您不想使用 seaborn
样式,但确实需要一些 seaborn 功能,您可以使用以下行 (documentation):
导入 seaborn
import seaborn.apionly as sns
如果你想生成一些带有 seaborn
风格的图,而另一些则没有,在同一个脚本中,你可以使用 seaborn.reset_orig
函数关闭 seaborn
风格。
似乎执行 apionly
导入本质上会在导入时自动设置 reset_orig
,所以这取决于您的使用情况。
这是在 matplotlib
默认值和 seaborn
之间切换的示例:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
# a simple plot function we can reuse (taken from the seaborn tutorial)
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 7):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sinplot()
# this will have the matplotlib defaults
plt.savefig('seaborn-off.png')
plt.clf()
# now import seaborn
import seaborn as sns
sinplot()
# this will have the seaborn style
plt.savefig('seaborn-on.png')
plt.clf()
# reset rc params to defaults
sns.reset_orig()
sinplot()
# this should look the same as the first plot (seaborn-off.png)
plt.savefig('seaborn-offagain.png')
生成以下三个图:
seaborn-off.png:
seaborn-on.png:
seaborn-offagain.png:
如前所述in this other question您可以导入seaborn:
import seaborn.apionly as sns
并且不会修改matplotlib样式。
您可以使用 style guide 中描述的 matplotlib.style.context
功能。
#%matplotlib inline #if used in jupyter notebook
import matplotlib.pyplot as plt
import seaborn as sns
# 1st plot
with plt.style.context("seaborn-dark"):
fig, ax = plt.subplots()
ax.plot([1,2,3], label="First plot (seaborn-dark)")
# 2nd plot
with plt.style.context("default"):
fig, ax = plt.subplots()
ax.plot([3,2,1], label="Second plot (matplotlib default)")
# 3rd plot
with plt.style.context("seaborn-darkgrid"):
fig, ax = plt.subplots()
ax.plot([2,3,1], label="Third plot (seaborn-darkgrid)")
允许将所有 RC 参数恢复为原始设置(尊重自定义 rc)
从 seaborn version 0.8 (July 2017) 开始,图形样式在导入时不再更改:
The default [seaborn] style is no longer applied when seaborn is imported. It is now necessary to explicitly call set()
or one or more of set_style()
, set_context()
, and set_palette()
. Correspondingly, the seaborn.apionly
module has been deprecated.
您可以通过plt.style.use()
选择任何情节的风格。
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn') # switch to seaborn style
# plot code
# ...
plt.style.use('default') # switches back to matplotlib style
# plot code
# ...
# to see all available styles
print(plt.style.available)
详细了解 plt.style()
。
Seaborn 提供了一些图形,这些图形对于科学数据表示非常有趣。 因此,我开始将这些 Seaborn 图形与其他定制的 matplotlib 图穿插使用。 问题是一旦我这样做了:
import seaborn as sb
这个导入似乎全局设置了seaborn的图形参数,然后导入下面的所有matplotlib图形都得到了seaborn参数(它们得到了灰色背景,linewithd变化等等)。
SO 中有 an answer 解释了如何使用 matplotlib 配置生成 seaborn 图,但我想要的是在同时使用两个库时保持 matplotlib 配置参数不变,同时能够生成,在需要时,原始的 seaborn 地块。
如果您不想使用 seaborn
样式,但确实需要一些 seaborn 功能,您可以使用以下行 (documentation):
import seaborn.apionly as sns
如果你想生成一些带有 seaborn
风格的图,而另一些则没有,在同一个脚本中,你可以使用 seaborn.reset_orig
函数关闭 seaborn
风格。
似乎执行 apionly
导入本质上会在导入时自动设置 reset_orig
,所以这取决于您的使用情况。
这是在 matplotlib
默认值和 seaborn
之间切换的示例:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
# a simple plot function we can reuse (taken from the seaborn tutorial)
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 7):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sinplot()
# this will have the matplotlib defaults
plt.savefig('seaborn-off.png')
plt.clf()
# now import seaborn
import seaborn as sns
sinplot()
# this will have the seaborn style
plt.savefig('seaborn-on.png')
plt.clf()
# reset rc params to defaults
sns.reset_orig()
sinplot()
# this should look the same as the first plot (seaborn-off.png)
plt.savefig('seaborn-offagain.png')
生成以下三个图:
seaborn-off.png:
seaborn-on.png:
seaborn-offagain.png:
如前所述in this other question您可以导入seaborn:
import seaborn.apionly as sns
并且不会修改matplotlib样式。
您可以使用 style guide 中描述的 matplotlib.style.context
功能。
#%matplotlib inline #if used in jupyter notebook
import matplotlib.pyplot as plt
import seaborn as sns
# 1st plot
with plt.style.context("seaborn-dark"):
fig, ax = plt.subplots()
ax.plot([1,2,3], label="First plot (seaborn-dark)")
# 2nd plot
with plt.style.context("default"):
fig, ax = plt.subplots()
ax.plot([3,2,1], label="Second plot (matplotlib default)")
# 3rd plot
with plt.style.context("seaborn-darkgrid"):
fig, ax = plt.subplots()
ax.plot([2,3,1], label="Third plot (seaborn-darkgrid)")
从 seaborn version 0.8 (July 2017) 开始,图形样式在导入时不再更改:
The default [seaborn] style is no longer applied when seaborn is imported. It is now necessary to explicitly call
set()
or one or more ofset_style()
,set_context()
, andset_palette()
. Correspondingly, theseaborn.apionly
module has been deprecated.
您可以通过plt.style.use()
选择任何情节的风格。
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn') # switch to seaborn style
# plot code
# ...
plt.style.use('default') # switches back to matplotlib style
# plot code
# ...
# to see all available styles
print(plt.style.available)
详细了解 plt.style()
。