最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python?Seaborn繪制統(tǒng)計圖全面指南(直方圖散點圖小提琴圖熱力圖相關(guān)系數(shù)圖多張合并)

 更新時間:2024年01月19日 09:55:07   作者:用戶007?Python學習雜記  
這篇文章主要介紹了python?Seaborn繪制統(tǒng)計圖全面指南,包括直方圖,散點圖,小提琴圖,熱力圖,相關(guān)系數(shù)圖及多張圖合并的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助

seaborn基礎(chǔ)使用

先看一一個簡單案例,

# 導入庫
import seaborn as sns
# 設(shè)置基本的配置
sns.set_theme()
# 導入數(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ù)需要開魔法,全局代理才能導入。如果無法導入數(shù)據(jù),也可以自己參照數(shù)據(jù)編寫樣本數(shù)據(jù)。數(shù)據(jù)樣式如下:

直方圖

可視化分布的最常見方法是直方圖。直方圖是一個條形圖,其中表示數(shù)據(jù)變量的軸被劃分為一組離散條柱,并且使用相應條形的高度顯示落在每個條柱內(nèi)的觀測值計數(shù):

penguins = sns.load_dataset("penguins")
sns.displot(penguins, x="flipper_length_mm")

也可以設(shè)置直方圖的寬度。

sns.displot(penguins, x="flipper_length_mm", binwidth=3)

散點圖

散點圖是指在回歸分析中,數(shù)據(jù)點在直角坐標系平面上的分布圖,散點圖表示因變量隨自變量而變化的大致趨勢,據(jù)此可以選擇合適的函數(shù)對數(shù)據(jù)點進行擬合。

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)點。小提琴圖通常用于比較多個組之間的分布差異,或者顯示一個變量在不同類別下的分布情況。

小提琴圖的外形類似于小提琴,中間部分是數(shù)據(jù)的密度估計曲線,兩側(cè)是箱線圖或者散點圖。小提琴圖的橫軸通常表示變量或者組別,縱軸表示數(shù)值變量的取值范圍。每個小提琴圖的寬度相同,高度表示數(shù)據(jù)的密度分布情況。

小提琴圖中的箱線圖表示數(shù)據(jù)的五數(shù)概括(最小值、下四分位數(shù)、中位數(shù)、上四分位數(shù)、最大值),箱線圖兩側(cè)的線條表示數(shù)據(jù)的范圍。如果需要比較多個組之間的分布差異,可以將它們放在同一個小提琴圖上進行比較。如果需要顯示一個變量在不同類別下的分布情況,可以將它們分別畫在不同的小提琴圖中進行比較。

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)計學家卡爾·皮爾遜設(shè)計的統(tǒng)計指標,是研究變量之間線性相關(guān)程度的量,一般用字母r表示。取值范圍為-1到1,小于0位負相關(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()

多張圖合并

有時候需要一次畫多個圖,需要用到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)計圖全面指南的詳細內(nèi)容,更多關(guān)于python Seaborn繪制統(tǒng)計圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 實現(xiàn)Mac 屏幕截圖詳解

    Python 實現(xiàn)Mac 屏幕截圖詳解

    今天小編就為大家分享一篇對Python 實現(xiàn)Mac 屏幕截圖詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-10-10
  • python實現(xiàn)每次處理一個字符的三種方法

    python實現(xiàn)每次處理一個字符的三種方法

    這篇文章主要介紹了python實現(xiàn)每次處理一個字符的三種方法,是非常實用的字符串操作技巧,需要的朋友可以參考下
    2014-10-10
  • PyQt5實現(xiàn)QLineEdit添加clicked信號的方法

    PyQt5實現(xiàn)QLineEdit添加clicked信號的方法

    今天小編就為大家分享一篇PyQt5實現(xiàn)QLineEdit添加clicked信號的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python xmlrpc踩坑記錄及解決方案

    python xmlrpc踩坑記錄及解決方案

    這篇文章主要介紹了python xmlrpc踩坑記錄及解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-11-11
  • python numpy linspace函數(shù)使用詳解

    python numpy linspace函數(shù)使用詳解

    本文介紹了Python Numpy庫中的linspace函數(shù),該函數(shù)用于生成均勻分布的數(shù)值序列,通過示例和詳細參數(shù)解釋,幫助讀者理解如何使用linspace函數(shù),最后,對比了linspace和arange函數(shù)之間的主要差異,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • pyecharts動態(tài)軌跡圖的實現(xiàn)示例

    pyecharts動態(tài)軌跡圖的實現(xiàn)示例

    這篇文章主要介紹了pyecharts動態(tài)軌跡圖的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • 三步快速實現(xiàn)Python解包EXE文件獲取源碼的完整教學

    三步快速實現(xiàn)Python解包EXE文件獲取源碼的完整教學

    你是否曾經(jīng)遇到過這樣的情況,需要分析一個Python打包的可執(zhí)行文件,但卻無法獲取原始代碼,Python EXE解包技術(shù)正是解決這個問題的關(guān)鍵,下面我們就來看看Python解包exe的具體步驟吧
    2026-01-01
  • Python實現(xiàn)Excel批量處理+郵件自動發(fā)送的全流程

    Python實現(xiàn)Excel批量處理+郵件自動發(fā)送的全流程

    在日常辦公中,重復的 Excel 數(shù)據(jù)整理、報表生成與郵件分發(fā)工作占據(jù)大量時間,本文將手把手教你用 Python 實現(xiàn) Excel 數(shù)據(jù)批量處理 + 自動化郵件發(fā)送全流程,需要的朋友可以參考下
    2025-12-12
  • Python+Turtle實現(xiàn)繪制勾股樹

    Python+Turtle實現(xiàn)繪制勾股樹

    畢達哥拉斯樹,也叫“勾股樹”,是由畢達哥拉斯根據(jù)勾股定理所畫出來的一個可以無限重復的樹形圖形。本文將利用Python中的Turtle庫實現(xiàn)勾股樹的繪制,感興趣的可以了解一下
    2023-01-01
  • Python中函數(shù)的參數(shù)類型詳解

    Python中函數(shù)的參數(shù)類型詳解

    這篇文章主要介紹了Python中函數(shù)的參數(shù)類型詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07

最新評論

锡林郭勒盟| 屏山县| 淄博市| 个旧市| 姚安县| 拉萨市| 南涧| 郎溪县| 桐庐县| 大埔区| 安徽省| 日喀则市| 西峡县| 鄂托克前旗| 裕民县| 广德县| 汤阴县| 恩施市| 资中县| 井陉县| 克东县| 南城县| 子长县| 措美县| 固始县| 双城市| 保定市| 徐水县| 伽师县| 贺兰县| 甘肃省| 江孜县| 资溪县| 股票| 鹤山市| 甘孜| 青阳县| 理塘县| 新和县| 读书| 浦城县|