2015-10-13 69 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風格。 –