天天看點

python可視化進階---seaborn1.9 時間線圖表、熱圖 tsplot() / heatmap()

時間線圖表、熱圖

tsplot() / heatmap()

1.時間線圖表 - tsplot()

示例1:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
 
#設定風格、尺度
sns.set_style('darkgrid')
sns.set_context('paper')
 
#不發出警告
import warnings
warnings.filterwarnings('ignore')
 
x = np.linspace(0, 15, 31)
data = np.sin(x) + np.random.rand(10,31) + np.random.randn(10,1)
print(data.shape)
print(pd.DataFrame(data).head())
 
sns.tsplot(data = data,
           err_style='ci_band', #誤差資料風格,可選:ci_band, ci_bars, boot_traces,
           #boot_kde, unit_traces, unit_points
           interpolate = True,  #設定連線
           ci = [40, 70, 90],   #設定誤差區間
           color = 'g'          #設定顔色
           )      
python可視化進階---seaborn1.9 時間線圖表、熱圖 tsplot() / heatmap()

示例2:

sns.tsplot(data = data, err_style = 'boot_traces',
           n_boot = 300   #疊代次數
           )      
python可視化進階---seaborn1.9 時間線圖表、熱圖 tsplot() / heatmap()

示例3:

#參數設定
#導入資料
gammas = sns.load_dataset('gammas')
print(gammas.head())
print('資料量為:%i條'%len(gammas))
print('timepoint為0.0時的資料量為:%i條'%len(gammas[gammas['timepoint'] == 0]))
#檢視唯一具體資訊
print('timepoint共有%i個唯一值'%len(gammas['timepoint'].value_counts()))
 
sns.tsplot(time = 'timepoint',    #時間資料, x軸
           value = 'BOLD signal', #y軸value
           unit = 'subject',      #拆分,預設參數
           condition = 'ROI',     #分類
           data = gammas
           )      
python可視化進階---seaborn1.9 時間線圖表、熱圖 tsplot() / heatmap()

2、熱圖 - heatmap()

df = pd.DataFrame(np.random.rand(10,12))
sns.heatmap(df, #加載資料
            vmin = 0, vmax = 1 #設定圖例最大最小值
            )      
python可視化進階---seaborn1.9 時間線圖表、熱圖 tsplot() / heatmap()

示例2:設定參數

#設定參數
#加載資料
flights = sns.load_dataset('flights')
flights = flights.pivot('month','year','passengers')
print(flights.head())
 
sns.heatmap(flights,
            annot = True,   #是否顯示數值
            fmt = 'd',      #格式化字元串
            linewidth = 0, #格子邊線寬度
            center = 100,   #調色盤的色彩中心值,若沒有指定,則以cmap為主
            cmap = 'Reds',  #設定調色盤
            cbar = True,     #是否顯示圖例色帶
            #bar_kws = ['orientaion':'horizaintal'], #是否橫向顯示圖例色帶
            #square = True   #是否正方形顯示圖表
            )      
python可視化進階---seaborn1.9 時間線圖表、熱圖 tsplot() / heatmap()

示例3:繪制半邊熱圖

#設定風格
sns.set(style = 'white')
#建立資料
rs = np.random.RandomState(33)
d = pd.DataFrame(rs.normal(size = (100, 26)))
corr = d.corr()  #求解相關性矩陣表格
#設定一個‘上三角形’蒙版
mask = np.zeros_like(corr, dtype = np.bool)
mask[np.triu_indices_from(mask)] = True
#設定調色盤
cmap = sns.diverging_palette(220, 10, as_cmap = True)
#生成半邊熱圖
sns.heatmap(corr, mask = mask, cmap = cmap, vmax = .3, center = 0,
            square = True, linewidths = 0.2)      
python可視化進階---seaborn1.9 時間線圖表、熱圖 tsplot() / heatmap()