數據可視化庫-Seaborn

import seaborn as sns
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline          #這句話的意思就是可以直接顯示圖
def sinplot(flip=1):
    x = np.linspace(0,14,100)           #(0,14) 取100點成圖
    for i in range(1,7):
        plt.plot(x,np.sin(x+i*.5)*(7-i)*flip)



sinplot()

整體風格設置 

sns.set()   #使用seaborn默認的參數
sinplot()

五種主題風格

1.darkgrid

2.whitegrid

3.dark

4.white

5.ticks

sns.set_style("whitegrid")
data = np.random.normal(size=(20,6))+np.arange(6)/2
#data = np.random.normal(size=(20,6))+np.arange(6)/2
sns.boxplot(data=data)

 

boxplot的意義: 

 

sns.set_style("dark")
sinplot()

 

風格細節設置

 

sns.set_style("whitegrid")
sns.boxenplot(data=data,palette='deep')
sns.despine(left=True)   #隱藏左邊的座標軸

 

#這裏指定一個with域,從而生成兩個子圖,兩種不同的風格
with sns.axes_style('darkgrid'):
    plt.subplot(211)
    sinplot()
plt.subplot(212)
sinplot(-1)

 

#另一種調節圖中線的粗細程度的風格:paper talk poster notebook,前三種風格的線越來越粗,最後一個notebook是可以通過參數的修改的

sns.set()  #使用默認設置
sns.set_context('paper')#圖片的佈局
plt.figure(figsize=(8,6))  #圖片的規模
sinplot()

 

sns.set_context("notebook", font_scale = 2, rc={"lines.linewidth" : 4.5}) #這個是可以的設置的參數的,font_scale是改變座標軸上的大小,rc中的line.linewidth是改變線的粗細

plt.figure(figsize=(8, 6))

sinplot()

 

調色板 

color_palette()能傳入任何matplotlib所支持的顏色

color_palette()不出參數則默認顏色

set_palette()設置所有圖的顏色

seaborn提供6個默認的顏色主題:deep,multed,pastel,bright,dark,colorblind

當你需要6個以上的顏色的時候,可以使用hls的顏色空間,這是RGB的一個簡單轉換。

current_palette = sns.color_palette()
sns.palplot(current_palette)

 

sns.palplot(sns.color_palette("hls",8))     #產生8種顏色

hls_palette()函數來控制顏色的亮度和飽和

l-亮度 lightness

s-飽和 saturation

sns.palplot(sns.hls_palette(8,l=.5,s=.9))

 

sns.palplot(sns.color_palette("Paired",8))  #成對出現的顏色

 

調色板顏色設置

使用xkcd顏色來命名顏色

xkcd包含了一套針對RGB色的命名,產生了954個可以隨時通過xkcd_rgb字典中調用的顏色命名。

 

plt.plot([0,3],[0,1],sns.xkcd_rgb['pale red'],lw=3)
plt.plot([0,1],[0,2],sns.xkcd_rgb['medium green'],lw=3)
plt.plot([0,1],[0,3],sns.xkcd_rgb['denim blue'],lw=3)

colors = {'windows blue','amber','greyish','faded green','dusty purple'}
sns.palplot(sns.xkcd_palette(colors))

 

連續色板

色彩隨着數據而變化,比如數據越來越重要,顏色越來越深

sns.palplot(sns.color_palette('Blues'))

 

sns.palplot(sns.color_palette('BuGn_r'))     #後面加_r表示由深入淺

 

sns.palplot(sns.color_palette('cubehelix',8))        #線性調色板

 

sns.palplot(sns.cubehelix_palette(8,start=.5,rot=-.75))

 

light_palette() 的dark_palette()調用定製連續調色板

sns.palplot(sns.dark_palette("Green"))

 

sns.palplot(sns.light_palette("Green"))

sns.palplot(sns.light_palette("Navy",reverse=True))  #設置反轉

 

x,y = np.random.multivariate_normal([0,0],[[1,-.5],[-.5,1]],size=300).T
pal = sns.dark_palette("Green",as_cmap=True)
sns.kdeplot(x,y,cmap=pal)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章