10分鐘python圖表繪製 | seaborn入門(三):Boxplot與Violinplot

Seaborn介紹

官方鏈接:Seaborn: statistical data visualization

Seaborn是一種基於matplotlib的圖形可視化python libraty。它提供了一種高度互動式界面,便於用戶能夠做出各種有吸引力的統計圖表。

Seaborn其實是在matplotlib的基礎上進行了更高級的API封裝,從而使得作圖更加容易,在大多數情況下使用seaborn就能做出很具有吸引力的圖,而使用matplotlib就能製作具有更多特色的圖。應該把Seaborn視為matplotlib的補充,而不是替代物。同時它能高度兼容numpy與pandas數據結構以及scipy與statsmodels等統計模式。掌握seaborn能很大程度幫助我們更高效的觀察數據與圖表,並且更加深入了解它們。

其有如下特點:

  • 基於matplotlib aesthetics繪圖風格,增加了一些繪圖模式
  • 增加調色板功能,利用色彩豐富的圖像揭示您數據中的模式
  • 運用數據子集繪製與比較單變數和雙變數分布的功能
  • 運用聚類演算法可視化矩陣數據
  • 靈活運用處理時間序列數據
  • 利用網格建立複雜圖像集

安裝seaborn

  1. 利用pip安裝

pip install seaborn

2. 在Anaconda環境下,打開prompt

conda install seaborn

boxplot箱線圖

seaborn.boxplot - seaborn 0.7.1 documentation

箱線圖——百度百科

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

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

主要包含五個數據節點,將一組數據從大到小排列,分別計算出他的上邊緣,上四分位數,中位數,下四分位數,下邊緣。

具體用法如下:

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琴形圖

seaborn.violinplot - seaborn 0.7.1 documentation

Violin plot - Wikipedia

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") #調整背景為白底import matplotlib.pyplot as pltplt.figure(figsize=(12, 6)) #由於變數過多,調整下圖表大小#繼續暗中觀察,攻擊與防禦分布如何ax1 = sns.violinplot(x="Type 1", y="Attack", data=pokemon, scale="width", palette="Set3")plt.figure(figsize=(12, 6))ax = sns.violinplot(x="Type 1", y="Defense", data=pokemon, scale="width", palette="Set3")

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

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

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

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

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

[download:pokemon數據集] 密碼:4zma

持續更新中,求贊求關注~謝謝


推薦閱讀:

當PowerBI遇到R語言
R語言信息可視化——文字雲
Excel史上最好看的堆積柱形圖
Excel繪製多種風格「條形圖」

TAG:Python | 数据可视化 | 数据 |