2015-10-13 70 views
14

Seaborn提供了一些对于科学数据表示非常有趣的图形。因此我开始使用这些穿插其他自定义matplotlib图的Seaborn图形。 的问题是,一旦我做的:Seaborn配置隐藏了默认的matplotlib

import seaborn as sb 

此导入似乎全局设置为seaborn图形参数,然后所有matplotlib图形导入下方得到seaborn参数(他们得到一个灰色的背景,linewithd变化等等)。

在所以an answer解释如何产生与matplotlib配置seaborn地块,但我要的是使用两个库在一起的时候,保持matplotlib配置参数不变,并在同一时间能够生产,在需要的时候,原来的海鸥地块。

+3

的可能的复制[我如何使用seaborn不改变matplotlib默认值?(http://stackoverflow.com/questions/25393936/how-can -i-use-seaborn-without-changing-the-matplotlib-defaults) – mwaskom

+0

seaborn文档非常好。这在安装文档的开头部分已经介绍过了:http://stanford.edu/~mwaskom/software/seaborn/installing.html?highlight=apionly#importing-seaborn –

+1

我不确定这是否与问题:它还解决了需要在脚本中动态切换seaborn和matplotlib默认值的问题,这不包括在建议的副本中 – tom

回答

16

如果您不想使用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-off

seaborn-on.png: seaborn-on

seaborn-offagain.png: enter image description here

+0

谢谢,reset_orig()是一种很好的方法,但它仍不能完全生成我的原始数字。我从两个不同的ipython笔记本中制作并保存了一张matplolib图(连续两个“plot(xi,yi,'。')”,其中xi和yi是一百个元素的列表),一个使用直接matplotlib,另一个使用直接matplotlib绘制重置为原始matplotlib条件。后seaborn图中的点的颜色明显不那么强烈,与未改变matplotlib配置的点相比(如果它们具有一些透明度)。 – joaquin

+2

也许最好分享一些代码,以便其他人可以重现该问题? – tom

0

正如所解释的in this other question您可以导入seaborn有:

import seaborn.apionly as sns 

而且matplotlib风格将不会被修改。

1

您可以使用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)") 

enter image description here

3

由于seaborn版本0.8(七月2017)的图形样式允许没有改变了上进口。 OP愿望现在是默认行为。来自https://seaborn.pydata.org/whatsnew.html

当seaborn被导入时,默认(seaborn)风格不再适用于。现在需要显式调用set_style(),set_context()和set_palette()的set()或一个或多个 。相应地,012bseaborn.apionly模块已被弃用。

您可以使用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()here

+0

我不得不做'plt.style.use('classic')'来获得默认的matplotlib风格。 –