python?Seaborn繪制統(tǒng)計(jì)圖全面指南(直方圖散點(diǎn)圖小提琴圖熱力圖相關(guān)系數(shù)圖多張合并)
seaborn基礎(chǔ)使用
先看一一個(gè)簡(jiǎn)單案例,
# 導(dǎo)入庫(kù) import seaborn as sns # 設(shè)置基本的配置 sns.set_theme() # 導(dǎo)入數(shù)據(jù) tips = sns.load_dataset("tips") # 可視化 sns.relplot( data=tips, x="total_bill", y="tip", col="time", hue="smoker", style="smoker", size="size", )
以上是用的seaborn自帶的樣例數(shù)據(jù),該數(shù)據(jù)需要開(kāi)魔法,全局代理才能導(dǎo)入。如果無(wú)法導(dǎo)入數(shù)據(jù),也可以自己參照數(shù)據(jù)編寫(xiě)樣本數(shù)據(jù)。數(shù)據(jù)樣式如下:
直方圖
可視化分布的最常見(jiàn)方法是直方圖。直方圖是一個(gè)條形圖,其中表示數(shù)據(jù)變量的軸被劃分為一組離散條柱,并且使用相應(yīng)條形的高度顯示落在每個(gè)條柱內(nèi)的觀測(cè)值計(jì)數(shù):
penguins = sns.load_dataset("penguins") sns.displot(penguins, x="flipper_length_mm")
也可以設(shè)置直方圖的寬度。
sns.displot(penguins, x="flipper_length_mm", binwidth=3)
散點(diǎn)圖
散點(diǎn)圖是指在回歸分析中,數(shù)據(jù)點(diǎn)在直角坐標(biāo)系平面上的分布圖,散點(diǎn)圖表示因變量隨自變量而變化的大致趨勢(shì),據(jù)此可以選擇合適的函數(shù)對(duì)數(shù)據(jù)點(diǎn)進(jìn)行擬合。
import seaborn as sns sns.set_theme(style="white") # Load the example mpg dataset mpg = sns.load_dataset("mpg") # Plot miles per gallon against horsepower with other semantics sns.relplot(x="horsepower", y="mpg", hue="origin", size="weight", sizes=(40, 400), alpha=.5, palette="muted", height=6, data=mpg)
小提琴圖
小提琴圖(violin plot)是一種用于可視化數(shù)值數(shù)據(jù)分布情況的圖表類型,它結(jié)合了箱線圖和核密度圖的優(yōu)點(diǎn)。小提琴圖通常用于比較多個(gè)組之間的分布差異,或者顯示一個(gè)變量在不同類別下的分布情況。
小提琴圖的外形類似于小提琴,中間部分是數(shù)據(jù)的密度估計(jì)曲線,兩側(cè)是箱線圖或者散點(diǎn)圖。小提琴圖的橫軸通常表示變量或者組別,縱軸表示數(shù)值變量的取值范圍。每個(gè)小提琴圖的寬度相同,高度表示數(shù)據(jù)的密度分布情況。
小提琴圖中的箱線圖表示數(shù)據(jù)的五數(shù)概括(最小值、下四分位數(shù)、中位數(shù)、上四分位數(shù)、最大值),箱線圖兩側(cè)的線條表示數(shù)據(jù)的范圍。如果需要比較多個(gè)組之間的分布差異,可以將它們放在同一個(gè)小提琴圖上進(jìn)行比較。如果需要顯示一個(gè)變量在不同類別下的分布情況,可以將它們分別畫(huà)在不同的小提琴圖中進(jìn)行比較。
import seaborn as sns import matplotlib.pyplot as plt sns.set_theme(style="whitegrid") # Load the example dataset of brain network correlations df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0) # Pull out a specific subset of networks used_networks = [1, 3, 4, 5, 6, 7, 8, 11, 12, 13, 16, 17] used_columns = (df.columns.get_level_values("network") .astype(int) .isin(used_networks)) df = df.loc[:, used_columns] # Compute the correlation matrix and average over networks corr_df = df.corr().groupby(level="network").mean() corr_df.index = corr_df.index.astype(int) corr_df = corr_df.sort_index().T # Set up the matplotlib figure f, ax = plt.subplots(figsize=(11, 6)) # Draw a violinplot with a narrower bandwidth than the default sns.violinplot(data=corr_df, bw_adjust=.5, cut=1, linewidth=1, palette="Set3") # Finalize the figure ax.set(ylim=(-.7, 1.05)) sns.despine(left=True, bottom=True)
熱力圖
import matplotlib.pyplot as plt import seaborn as sns sns.set_theme() # Load the example flights dataset and convert to long-form flights_long = sns.load_dataset("flights") flights = ( flights_long .pivot(index="month", columns="year", values="passengers") ) # Draw a heatmap with the numeric values in each cell f, ax = plt.subplots(figsize=(9, 6)) sns.heatmap(flights, annot=True, fmt="d", linewidths=.5, ax=ax)
相關(guān)系數(shù)圖
相關(guān)系數(shù)是最早由統(tǒng)計(jì)學(xué)家卡爾·皮爾遜設(shè)計(jì)的統(tǒng)計(jì)指標(biāo),是研究變量之間線性相關(guān)程度的量,一般用字母r表示。取值范圍為-1到1,小于0位負(fù)相關(guān),大于0為正相關(guān)。
import pandas as pd import seaborn as sns sns.set_theme() # Load the brain networks example dataset df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0) # Select a subset of the networks used_networks = [1, 5, 6, 7, 8, 12, 13, 17] used_columns = (df.columns.get_level_values("network") .astype(int) .isin(used_networks)) df = df.loc[:, used_columns] # Create a categorical palette to identify the networks network_pal = sns.husl_palette(8, s=.45) network_lut = dict(zip(map(str, used_networks), network_pal)) # Convert the palette to vectors that will be drawn on the side of the matrix networks = df.columns.get_level_values("network") network_colors = pd.Series(networks, index=df.columns).map(network_lut) # Draw the full plot g = sns.clustermap(df.corr(), center=0, cmap="vlag", row_colors=network_colors, col_colors=network_colors, dendrogram_ratio=(.1, .2), cbar_pos=(.02, .32, .03, .2), linewidths=.75, figsize=(12, 13)) g.ax_row_dendrogram.remove()
多張圖合并
有時(shí)候需要一次畫(huà)多個(gè)圖,需要用到FacetGrid模塊。
import numpy as np import pandas as pd import seaborn as sns sns.set_theme() # Generate an example radial datast r = np.linspace(0, 10, num=100) df = pd.DataFrame({'r': r, 'slow': r, 'medium': 2 * r, 'fast': 4 * r}) # Convert the dataframe to long-form or "tidy" format df = pd.melt(df, id_vars=['r'], var_name='speed', value_name='theta') # Set up a grid of axes with a polar projection g = sns.FacetGrid(df, col="speed", hue="speed", subplot_kws=dict(projection='polar'), height=4.5, sharex=False, sharey=False, despine=False) # Draw a scatterplot onto each axes in the grid g.map(sns.scatterplot, "theta", "r")
以上就是python Seaborn繪制統(tǒng)計(jì)圖全面指南的詳細(xì)內(nèi)容,更多關(guān)于python Seaborn繪制統(tǒng)計(jì)圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python用for循環(huán)求和的方法總結(jié)
在本篇文章里小編給各位分享了關(guān)于python用for循環(huán)求和的方法以及相關(guān)實(shí)例代碼,需要的朋友們參考學(xué)習(xí)下。2019-07-07M1 mac安裝PyTorch的實(shí)現(xiàn)步驟
本文將介紹如何在M1機(jī)器上本地安裝和運(yùn)行PyTorch。你使用的M1機(jī)型(Air、Pro、Mini或iMac)沒(méi)有區(qū)別。感興趣的可以了解一下2021-08-08Python實(shí)現(xiàn)批量識(shí)別圖片文字并存為Excel
批量文字識(shí)別是Python辦公自動(dòng)化的基本操作,應(yīng)用在我們工作生活中的方方面面。本文主要以開(kāi)源免費(fèi)的easyocr來(lái)實(shí)現(xiàn)批量識(shí)別圖片文字并存為Excel,感興趣的可以學(xué)習(xí)一下2022-06-06scrapy爬蟲(chóng):scrapy.FormRequest中formdata參數(shù)詳解
這篇文章主要介紹了scrapy爬蟲(chóng):scrapy.FormRequest中formdata參數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Python 之pandas庫(kù)的安裝及庫(kù)安裝方法小結(jié)
Pandas 是一種開(kāi)源的、易于使用的數(shù)據(jù)結(jié)構(gòu)和Python編程語(yǔ)言的數(shù)據(jù)分析工具,它與 Scikit-learn 兩個(gè)模塊幾乎提供了數(shù)據(jù)科學(xué)家所需的全部工具,今天通過(guò)本文給大家介紹Python 之pandas庫(kù)的安裝及庫(kù)安裝方法小結(jié),感興趣的朋友跟隨小編一起看看吧2022-11-11python實(shí)現(xiàn)文本進(jìn)度條 程序進(jìn)度條 加載進(jìn)度條 單行刷新功能
這篇文章主要介紹了python實(shí)現(xiàn)文本進(jìn)度條 程序進(jìn)度條 加載進(jìn)度條 單行刷新功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解
今天小編就為大家分享一篇關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01