用seaborn對數據可視化

如下用sns做爲seaborn的別名函數

1.seaborn總體佈局設置佈局

  sns.set_syle()函數設置圖的風格,傳入的參數能夠是"darkgrid", "whitegrid", "dark", "white", "ticks", 分別表明五種風格。sns.despine()能夠去掉右邊和上面的邊線。
spa

下面的代碼畫出五種風格的圖code

 1 import seaborn as sns
 2 import numpy as np
 3 import matplotlib as mpl
 4 import matplotlib.pyplot as plt
 5 
 6 
 7 def sinplot(ax):
 8     x = np.linspace(0, 14, 100)
 9     for i in range(6):
10         y = np.sin(x+i*5)*(7-i)
11         ax.plot(x, y)
12 
13 
14 style = ["darkgrid", "whitegrid", "dark", "white", "ticks"]
15 print(style[0])
16 
17 plt.figure(figsize=(10, 10))
18 for i in range(5):
19     sns.set_style(style[i])   #設置樣式必定要在子圖的定義以前!!!!!!!
20     ax = plt.subplot(2, 3, i+1)
21     ax.set_title(style[i])
22     sinplot(ax)
23 
24 plt.show()

運行結果以下blog

 2.關於seaborn設置樣式是針對哪一個圖形區(subplot)的問題it

下面是我作的一個實驗性的代碼class

 1 import seaborn as sns
 2 import numpy as np
 3 import matplotlib as mpl
 4 import matplotlib.pyplot as plt
 5 
 6 
 7 fig = plt.figure(figsize=(18,10))
 8 x = np.linspace(0, 6*np.pi, 100)
 9 y = np.sin(x)
10 ax_sin = plt.subplot(1, 2, 1)
11 sns.set()               #根據就近原則,這裏的set操做是針對最近未定義的圖形區ax_cos的
12 sns.despine(offset=100)     #根據就近原則,這裏的despine操做是針對最近定義的圖形區ax_cos的
13 plt.plot(x, y)             #根據就近原則,這裏的plot操做是針對最近定義的圖形區ax_sin的
14 z = np.cos(x)
15 ax_cos = plt.subplot(1, 2, 2)
16 plt.plot(x, z)  #根據就近原則,這裏的plot操做是針對最近定義的圖形區ax_cos的
17 plt.show()

  運行結果以下,根據運行結果能夠推測,seaborn的despine操做和pyplot的plot操做都是在最近已經定義的圖形區上,例如代碼12,13行是在ax_sin上操做的,而11行的set是在即將定義的圖形區上操做,import

即ax_cos上操做。至於爲何會有這個規律以及有沒有相關總結,關於哪些操做是在最近已經定義的圖形區上仍是在即將定義的圖形區上進行暫時我還不清楚,但願有大神能指點一下。grid

相關文章
相關標籤/搜索