1. 程式人生 > >視覺化Seaborn1風格和背景

視覺化Seaborn1風格和背景

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import seaborn as sns
import numpy as np
#import matplotlib as mpl
import matplotlib.pyplot as plt
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()
sns.set()
sinplot()
#五種主題的風格:
#darkgrid  whitegrid  dark  white  ticks
sns.set_style(
"whitegrid") #背景風格 data = np.random.normal(size=(20,6))+np.arange(6)/2 sns.boxplot(data=data) sns.set_style("ticks") sinplot() sns.set_style("whitegrid") sns.boxplot(data=data,palette="deep") sns.despine(left=True) #左邊的軸隱藏起來 with sns.axes_style("darkgrid"): #子圖,不同風格 plt.subplot(211) sinplot() plt.subplot(
212) sinplot(-1) sns.set_context("poster") plt.figure(figsize=(8, 6)) #指定大小 sinplot() sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5}) sinplot() #2. 顏色 #•顏色很重要 #color_palette()能傳入任何Matplotlib所支援的顏色 #color_palette()不寫引數則預設顏色 #set_palette()設定所有圖的顏色 current_plette = sns.color_palette() #分類色版,預設6種 sns.palplot(current_plette) #當你有六個以上的分類要區分時, # 最簡單的方法就是在一個圓形的顏色空間中畫出均勻間隔的顏色 #使用hls的顏色空間,這是RGB值的一個簡單轉換 sns.palplot(sns.color_palette(
"hls",8))#8種顏色 data = np.random.normal(size=(20,8))+np.arange(8)/2 sns.boxplot(data=data,palette=sns.color_palette("his",8)) #hls_palette()函式來控制顏色的亮度和飽和 #l-亮度 lightness #s-飽和 saturation sns.palplot(sns.hls_palette(8, l=.7, s=.9)) sns.palplot(sns.hls_palette("Paired",8))#調出一對相近顏色 #使用xkcd顏色來命名顏色 plt.plot([0, 1], [0, 1], sns.xkcd_rgb["pale red"], lw=3) #色彩隨資料變換,比如資料越來越重要則顏色越來越深 sns.palplot(sns.color_palette("Blues")) #翻轉漸變,在名稱中新增一個_r字尾 sns.palplot(sns.color_palette("BuGn_r")) #cubehelix_palette()調色盤,色調線性變換 sns.palplot(sns.color_palette("cubehelix",8)) #light_palette() 和dark_palette()呼叫定製連續調色盤 # #reverse:翻轉 sns.palplot(sns.light_palette("green",reverse=True)) #3.資料分析