圖表可視化--Seaborn

基於matplatlib的python數據可視化庫,提升更高層次的API封裝,包括一些高級圖表可視化等工具。javascript

 

 主要技術點

  • 風格及調色盤
  • 分佈數據可視化
  • 分類數據可視化
  • 線性關係數據可視化
  • 其餘圖標可視化
  • 結構化圖表可視化

 

 風格及調色盤  

 

 

In [ ]:
"""
對圖表總體顏色、比例等進行風格設置,包括顏色色板等
調用系統風格進行數據可視化


set()/set_style()/axes_style()/despine()/set_context()
"""
In [9]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
In [10]:
# 建立正弦函數以及圖表

def sinplot(flip=1):
    x = np.linspace(0,14,100)
    for i in range(1,7):
        plt.plot(x,np.sin(x+i*0.5)*(7-i)*flip)
sinplot()
# 建立正弦函數,以及出圖
 
In [11]:
sns.set()   # 一旦設置,全局都回更改,只有重啓能夠修改
def sinplot(flip=1):
    x = np.linspace(0,14,100)
    for i in range(1,7):
        plt.plot(x,np.sin(x+i*0.5)*(7-i)*flip)
sinplot()
plt.grid(linestyle='--')
# 建立正弦函數,以及出圖
 
In [12]:
# 二、set_style()
# 設置seaborn圖表風格 white    dark    whitegrid  darkgrid  ticks
fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(2,1,1)
sns.set_style('darkgrid')     # 修改須要重啓
data = np.random.normal(size=(10,6))+np.arange(6)/2

sns.boxplot(data=data)

plt.title('style - whitegrid')

ax2 = fig.add_subplot(2,1,2)
sns.set_style('dark')
sinplot() # 子圖顯示      仍然能夠使用原來的
 
In [13]:
# despine()
# 設置圖表座標軸
"""
sns.despine(
    ['fig=None', 'ax=None', 'top=True', 'right=True', 'left=False', 'bottom=False', 'offset=None', 'trim=False'],
) 
"""
sns.set_style('ticks')# 設置風格

fig = plt.figure(figsize=(10,6))
plt.subplots_adjust(hspace=0.3)
# 建立圖表

ax1 = fig.add_subplot(3,1,1)
sinplot()
sns.despine()
"""
全局做用
sns.despine( 
    ['fig=None', 'ax=None', 'top=True', 'right=True', 'left=False', 'bottom=False', 'offset=None', 'trim=False'],
)
offset  與座標軸之間的偏移
trim 爲True時,將座標軸限制在數據最大最小值
top、。。bottom 布爾型,爲True時不顯示,默認是不顯示上右

"""



with sns.axes_style('darkgrid'): # 局部設置風格
    ax2 = fig.add_subplot(3,1,2)
    sns.violinplot(data=data)  # 小提琴圖
     




ax3 = fig.add_subplot(3,1,3)
sns.boxplot(data=data,palette='deep')# 直方圖
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x189501a7f60>
 
In [14]:
# set_context()
# 設置顯示比例尺度
"""
sns.set_context(context=None, font_scale=1, rc=None)
context    :    paper, notebook, talk, poster
"""

sns.set_context('notebook')
sinplot()
 
In [ ]:
 
In [ ]:
相關文章
相關標籤/搜索