日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

【seaborn】(1) 数据可视化,绘图风格、布局

發布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【seaborn】(1) 数据可视化,绘图风格、布局 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

各位同學好,今天和大家分享一下如何使用 seaborn 庫進行數據可視化。在 matplotlib 的基礎上進一步美化繪圖。主要內容有:默認風格 sns.set(), 主題風格 sns.set_style(), 邊框控制?sns.despine(),??局部圖表風格 axes_style(),?繪圖樣式設置 sns.set_context()


1. 默認風格設置?sns.set()

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns# matplot繪制正弦函數
x = np.linspace(0, 14, 100)  # 在0-14之間取出100個點# 繪制5條正弦線
plt.figure()
for i in range(5):plt.plot(x, np.sin(x+i*0.5)*(7-i))# 設置繪圖畫風組合,調用seaborn的模板庫
plt.figure()
sns.set()  # 使用seaborn默認的繪圖風格
for i in range(5):plt.plot(x, np.sin(x+i*0.5)*(7-i))

左圖為原始曲線,右圖為設置畫風后的曲線

?


2. 主題風格?sns.set_style()

seaborn 庫提供了五種繪圖風格,分別是:darkgrid(灰色網格),whitegrid(白色網格),dark(深色),white(白色),ticks(刻度線段)

在繪圖之前,先指定圖像的主題風格,sns.set_style( 'darkgrid' ),如下。

plt.figure()  # 新建畫圖板
sns.set_style('darkgrid')  # 設置風格
data = np.random.normal(size=(20,6)) + np.arange(6)/2 # 自定義數據
sns.boxplot(data=data) # 繪制盒圖
plt.title('darkgrid')  # 設置標題

接下來繪制其他四種主題風格

fig = plt.figure(figsize=(10,8))  # 創建新畫板
styleList = ['whitegrid', 'dark', 'white', 'ticks']  # 設置其他幾種風格# 循環繪制其他幾種風格
for i, style in enumerate(styleList):sns.set_style(style)  # 先設置風格plt.subplot(2,2,i+1)  # 再指定當前圖的位置data = np.random.normal(size=(20,6)) + np.arange(6)/2 # 自定義數據sns.boxplot(data=data) # 繪制盒圖plt.title(style)  # 設置標題fig.tight_layout()  #自動調節布局重疊問題 


3. 函數 despine() 控制邊框

3.1 刪除上側和右側邊框 sns.despine()

設置繪圖的主題風格為 ticks,即每個坐標軸都有刻度線段,如上圖中的右下圖。通過 sns.despine() 刪除圖像上的上側和右側的坐標軸線。

#(3)去除繪圖框上邊緣和右邊緣的刻度線
plt.figure(figsize=(10,5))  # 新建畫板
x = np.linspace(0, 14, 100)  # 設置x軸,0-14之間取100個數
sns.set_style('ticks')   # 設置繪圖風格# 繪制五條正弦曲線
for i in range(5):plt.plot(x, np.sin(x+i*0.5)*(7-i))
# 刪除上側和右側邊緣線
sns.despine()
# 設置標題
plt.title('despine')


3.2 邊框位移?sns.despine(offset, trim)

使用 sns.despine(offset, trim) 函數完成邊框位移,其中參數:?offset 代表x和y坐標軸偏移量trim=False 代表坐標軸沒有限制; trim=True?將坐標軸限制在數據最大最小值之間

如下,通過設置 sns.despine(offset=50, trim=True) ,使圖像距離x軸和y軸的距離都是50,而x軸的坐標軸刻度范圍是從0到14,y軸的坐標軸刻度范圍是從-6到6

# 設置x軸刻度
x = np.linspace(0, 14, 100) # 在0-14之間取出100個點
fig = plt.figure() # 新建畫圖板plt.subplot(2,1,1) # 繪圖位置
# 移動坐標后的曲線
for i in range(5):plt.plot(x, np.cos(x+i*0.5)*(7-i))
# 移動軸線
sns.despine(offset=50, trim=True)  # 圖像與x軸和y軸之間的距離
# 設置標題
plt.title('trim=True')plt.subplot(2,1,2) # 繪圖位置
# 移動坐標后的曲線
for i in range(5):plt.plot(x, np.cos(x+i*0.5)*(7-i))
# 移動軸線
sns.despine(offset=50)  # 圖像與x軸和y軸之間的距離
# 設置標題
plt.title('trim=False')fig.tight_layout()  #輕量化布局


3.3 自定義隱藏邊框 sns.despine(right, left, top, bottom)?

使用 sns.despine() 默認參數可以隱藏圖像的上側和右側的邊緣框,如果想自定義隱藏邊界框,可以設置參數,sns.despine(right=True, left=False, top=True, bottom=False) 同樣能實現隱藏上側和右側邊界框

sns.set_style('ticks')  # 設置風格
data = np.random.normal(size=(20,6)) + np.arange(6)/2 # 自定義數據plt.figure()  # 繪制原始圖
sns.boxplot(data=data) # 繪制盒圖
plt.title('origin')  # 設置標題plt.figure()  # 繪制原始圖
sns.boxplot(data=data) # 繪制盒圖
sns.despine(right=True, left=True, top=False, bottom=False)  # 隱藏上邊緣和左邊緣的刻度線
plt.title('despined')  # 設置標題

第一張為風格為ticks的原圖,第二張為隱藏左側和右側邊框后的圖


4. 局部圖表風格?axes_style()?

axes_style() 設置局部圖表風格和 with 函數配合的用法,在當前 with 域中使用指定的圖像的風格,在 with 域外可以使用其他風格。

如下,以 with 域作為分隔,with域內的繪圖使用 'darkgrid' 風格,而 with 域外的繪圖使用?'whitegrid' 的風格。

fig = plt.figure() # 新建畫板
x = np.linspace(0, 14, 100) # 設置x軸刻度# 使用with打開一種風格,在with里面執行的都是該風格
with sns.axes_style('darkgrid'):  # 指定風格plt.subplot(2,1,1)  # 繪圖位置# 繪制正弦曲線for i in range(5):plt.plot(x, np.sin(x+i*0.5)*(7-i))# 設置標題plt.title('darkgrid')# with域外部繪圖就是另外一種風格
sns.axes_style('whitegrid') 
plt.subplot(2,1,2)  # 繪圖位置
# 繪制正弦曲線
for i in range(5):plt.plot(x, np.sin(x+i*0.5)*(7-i))# 設置標題
plt.title('darkgrid')fig.tight_layout()  #輕量化布局


5. 繪圖樣式設置?sns.set_context()

?如果想要定制 seanborn 的樣式,可以參數字典傳遞給 axes_style() set_style() rc 參數。通過 sns.axes_style() 查看當前繪圖的格式參數設置。

sns.axes_style()
# 返回結果
'''
{'axes.facecolor': 'white','axes.edgecolor': '.15','axes.grid': False,'axes.axisbelow': True,'axes.labelcolor': '.15','figure.facecolor': 'white','grid.color': '.8','grid.linestyle': '-','text.color': '.15','xtick.color': '.15','ytick.color': '.15','xtick.direction': 'out','ytick.direction': 'out','lines.solid_capstyle': 'round','patch.edgecolor': 'w','patch.force_edgecolor': True,'image.cmap': 'rocket','font.family': ['sans-serif'],'font.sans-serif': ['Arial','DejaVu Sans','Liberation Sans','Bitstream Vera Sans','sans-serif'],'xtick.bottom': True,'xtick.top': False,'ytick.left': True,'ytick.right': False,'axes.spines.left': True,'axes.spines.bottom': True,'axes.spines.right': True,'axes.spines.top': True}
'''

接下來就可以根據自己的需要來修改這些繪圖樣式參數。

sns.set_context(context, font_scale=1, rc=None) ?

其中,context 代表圖像大小,選項有 [ 'paper', 'talk', 'poster', 'notebook' ]font_scale 代表字體大小 rc 代表繪圖樣式參數。

plt.figure()  # 創建畫板
x = np.linspace(0, 14, 100) # 在0-14之間取出100個點
# 設置布局格式, 字體大小, 線條粗細
sns.set_context('paper')  
# 繪制正弦曲線
for i in range(5):plt.plot(x, np.sin(x+i*0.5)*(7-i))# 設置標題
plt.title('original paper style')# 在paper風格基礎上編輯
plt.figure()  # 創建畫板
# 設置布局格式, 字體大小, 線條粗細
sns.set_context('paper', font_scale=3, rc={'lines.linewidth':3})  
# 繪制正弦曲線
for i in range(5):plt.plot(x, np.sin(x+i*0.5)*(7-i))# 設置標題
plt.title('changed paper style')

左圖為原圖正弦曲線,右圖為更改樣式后的曲線

?

總結

以上是生活随笔為你收集整理的【seaborn】(1) 数据可视化,绘图风格、布局的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。