您的位置:首頁>正文

10分鐘 Python seaborn 繪圖入門 (Ⅲ)

感謝關注天善智慧, 走好資料之路↑↑↑

歡迎關注天善智慧, 我們是專注于商業智慧BI, 大資料, 資料分析領域的垂直社區, 學習, 問答、求職一站式搞定!

每週2-3場免費資料領域公開課, 每月線下活動, 歡迎關注!

boxplot 箱線圖

箱線圖——百度百科

"盒式圖" 或叫 "盒須圖" "箱形圖",, 其繪製須使用常用的統計量, 能提供有關資料位置和分散情況的關鍵資訊, 尤其在比較不同的母體資料時更可表現其差異。

如上圖所示, 標示了圖中每條線表示的含義, 其中應用到了分位值(數)的概念。

主要包含五個資料節點, 將一組資料從大到小排列,

分別計算出他的上邊緣, 上四分位數, 中位數, 下四分位數, 下邊緣。

具體用法如下:

seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)

Parameters:

x, y, hue : names of variables in data or vector data, optional #設置 x,y 以及顏色控制的變數

Inputs for plotting long-form data. See examples for interpretation.

data : DataFrame, array, or list of arrays, optional #設置輸入的資料集

Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.

order, hue_order : lists of strings, optional #控制變數繪圖的順序

Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.

import seaborn as snssns.set_style("whitegrid")tips = sns.load_dataset("tips") #載入自帶資料集#研究三個變數之間的關係,是否抽煙與日期為分類變數,消費是連續變數#結論發現吸煙者在週末消費明顯大於不吸煙的人ax = sns.boxplot(x="day", y="total_bill", hue="smoker",data=tips, palette="Set3")

Senior Example Ⅰ for Practice

boxplot 探索 Pokemon 寵物小精靈的屬性分佈。

屬性科普

至最新的精靈寶可夢遊戲第七世代 “精靈寶可夢太陽 / 月亮”, 總共 18 種屬性, 分別是:一般、火、水、草、電、冰、蟲、飛行、地面、岩石、格鬥、超能力、幽靈、毒、惡、鋼、龍、妖精。 不僅精靈寶可夢(台譯神奇寶貝, 港譯寵物小精靈)有屬性, 招式也有其屬性, 而其威力則以乘數計。 攻擊優勢屬性時, 可以到雙倍甚至四倍計算, 攻擊劣勢屬性時, 攻擊威力減半甚至無效。

單一屬性的計算如圖, 而雙屬性計算方法是 2 個屬性受到的加成比相乘, 例如:使用火系技能攻擊岩石系和鋼系會造成 1 倍傷害(2X0.5=1);使用火系技能攻擊水系和龍系雙屬性的敵人會造成 1/4 的傷害(0.5X0.5=0.25);使用火系技能攻擊蟲系和草系雙屬性的敵人會造成 4 倍傷害(2X2=4);使用格鬥系的技能攻擊鬼系敵人無效 (2X0=0)。

特性和技能的特殊效果會影響傷害計算, 例如:使用技能冷凍乾燥會對水屬性造成雙倍傷害;嗅覺可以使普通系技能命中鬼屬性, 並造成 1 倍傷害

# -*-coding:utf-8 -*-import pandas as pdimport seaborn as snspokemon=pd.read_csv('H:/zhihu/Pokemon.csv',)#每列分別是名稱,第一屬性,第二屬性,總數,血量,攻擊,防禦,特殊攻擊,特殊防禦,速度,代數,傳奇pokemon.head()

#觀察各屬性的分佈pkmn = pokemon.drop(['Total', '#','Generation','Legendary'],1);sns.boxplot(data=pkmn);

HP 值普遍較低,

但是 HP 大於 120 肉盾異常值倒是挺多

#牛刀小試, 第一屬性與HP值/speed速度分佈關係sns.boxplot(y='Type 1',x='HP',data=pokemon)sns.boxplot(y='Type 1',x='Speed',data=pokemon)

FLYING 飛行類速度一騎絕塵

Violinplot 琴形圖

Violinplot 結合了箱線圖與核密度估計圖的特點,它表徵了在一個或多個分類變數情況下,連續變數資料的分佈並進行了比較,它是一種觀察多個資料分佈有效方法。

seaborn.violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)

split : bool, optional #琴形圖是否從中間分開兩部分

When using hue nesting with a variable that takes two levels, setting split to True will draw half of a violin for each level. This can make it easier to directly compare the distributions.

scale : {“area”, “count”, “width”}, optional #用於調整琴形圖的寬頻。area——每個琴圖擁有相同的面域;count——根據樣本數量來調節寬度;width——每個琴圖則擁有相同的寬度。

The method used to scale the width of each violin. If area, each violin will have the same area. If count, the width of the violins will be scaled by the number of observations in that bin. If width, each violin will have the same width.

inner : {“box”, “quartile”, “point”, “stick”, None}, optional #控制琴圖內部資料點的形態。box——繪製微型 boxplot;quartiles——繪製四分位的分佈;point/stick——繪製點或小豎條。

Representation of the datapoints in the violin interior. If box, draw a miniature boxplot. If quartiles, draw the quartiles of the distribution. If point or stick, show each underlying datapoint. Using None will draw unadorned violins.

Senior Example Ⅱ for Practice

#以速度為y軸,世代為x軸區分"傳奇",來繪製攻擊能力的分佈圖

#由於傳奇系很稀少,scale選擇width,保持兩邊寬度相同,inder選擇stick加入分佈豎條

sns.violinplot(y='Attack',x='Generation',data=pokemon,hue='Legendary',palette="Set3",split=True,scale="width",
inner="stick", scale_hue=False)

世代並沒有影響攻擊能力的差異,傳奇系的生物倒是攻擊能力超高!!雖然我也不懂什麼 Legendary,但是看起來確實很厲害的樣子

sns.set_style("whitegrid") #調整背景為白底

Dragon 龍類生物攻擊十分強勢,出來吧噴火龍

Steel 鋼屬性生物則是肉盾型,皮糙肉厚,著名代表大鋼蛇

好奇觀察一波皮卡丘的屬性與電屬性生物的情況

pokemon[pokemon['Name']=='Pikachu']sns.boxplot(data=pkmn[pkmn['Type 1']=='Electric'])

皮卡丘這渣屬性在同類中還達不到平均水準 ,為什麼 = =

天善學院svip正火爆報名中!包含業務知識一站通、Excel BI商業智慧、七周成為資料分析師、對話大資料數列技術、R語言15案例、Python3網路爬蟲實戰案例、Python機器學習、Python資料科學家精華實戰課程、深度學習模型和實戰課程、資料分析報告共10套課程,其他課程只需五折即可,歡迎大家關注報名。https://www.hellobi.com/svip

本文作者:知行EasyCharts

Violinplot 琴形圖

Violinplot 結合了箱線圖與核密度估計圖的特點,它表徵了在一個或多個分類變數情況下,連續變數資料的分佈並進行了比較,它是一種觀察多個資料分佈有效方法。

seaborn.violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)

split : bool, optional #琴形圖是否從中間分開兩部分

When using hue nesting with a variable that takes two levels, setting split to True will draw half of a violin for each level. This can make it easier to directly compare the distributions.

scale : {“area”, “count”, “width”}, optional #用於調整琴形圖的寬頻。area——每個琴圖擁有相同的面域;count——根據樣本數量來調節寬度;width——每個琴圖則擁有相同的寬度。

The method used to scale the width of each violin. If area, each violin will have the same area. If count, the width of the violins will be scaled by the number of observations in that bin. If width, each violin will have the same width.

inner : {“box”, “quartile”, “point”, “stick”, None}, optional #控制琴圖內部資料點的形態。box——繪製微型 boxplot;quartiles——繪製四分位的分佈;point/stick——繪製點或小豎條。

Representation of the datapoints in the violin interior. If box, draw a miniature boxplot. If quartiles, draw the quartiles of the distribution. If point or stick, show each underlying datapoint. Using None will draw unadorned violins.

Senior Example Ⅱ for Practice

#以速度為y軸,世代為x軸區分"傳奇",來繪製攻擊能力的分佈圖

#由於傳奇系很稀少,scale選擇width,保持兩邊寬度相同,inder選擇stick加入分佈豎條

sns.violinplot(y='Attack',x='Generation',data=pokemon,hue='Legendary',palette="Set3",split=True,scale="width",
inner="stick", scale_hue=False)

世代並沒有影響攻擊能力的差異,傳奇系的生物倒是攻擊能力超高!!雖然我也不懂什麼 Legendary,但是看起來確實很厲害的樣子

sns.set_style("whitegrid") #調整背景為白底

Dragon 龍類生物攻擊十分強勢,出來吧噴火龍

Steel 鋼屬性生物則是肉盾型,皮糙肉厚,著名代表大鋼蛇

好奇觀察一波皮卡丘的屬性與電屬性生物的情況

pokemon[pokemon['Name']=='Pikachu']sns.boxplot(data=pkmn[pkmn['Type 1']=='Electric'])

皮卡丘這渣屬性在同類中還達不到平均水準 ,為什麼 = =

天善學院svip正火爆報名中!包含業務知識一站通、Excel BI商業智慧、七周成為資料分析師、對話大資料數列技術、R語言15案例、Python3網路爬蟲實戰案例、Python機器學習、Python資料科學家精華實戰課程、深度學習模型和實戰課程、資料分析報告共10套課程,其他課程只需五折即可,歡迎大家關注報名。https://www.hellobi.com/svip

本文作者:知行EasyCharts

同類文章
Next Article
喜欢就按个赞吧!!!
点击关闭提示