Python数据可视化:如何创建箱线图
一圖勝千言,使用Python的matplotlib庫(kù),可以快速創(chuàng)建高質(zhì)量的圖形。
我們團(tuán)隊(duì)推出一個(gè)新的系列教程:Python數(shù)據(jù)可視化,針對(duì)初級(jí)和中級(jí)用戶,將理論和示例代碼相結(jié)合,使用matplotlib, seaborn, plotly等工具實(shí)現(xiàn)可視化。
本文的主題是如何用Matplotlib創(chuàng)建箱線圖(boxplot)。
import numpy as np import matplotlib.pyplot as plt%matplotlib inlineplt.style.use("ggplot")1. 創(chuàng)建基礎(chǔ)箱線圖
箱線圖(box-plot): 描述數(shù)值變量分布的統(tǒng)計(jì)圖表,使用的樣本統(tǒng)計(jì)量包括最小值,最大值,中位數(shù),25%分位數(shù)(下四分位數(shù)),75%分位數(shù)(上四分位數(shù))。在箱線圖中,x軸表示數(shù)據(jù)的類別,y軸表示數(shù)據(jù)的分布。
Matplotlib創(chuàng)建箱線圖的接口:boxplot(x, notch, vert, patch_artist, whis, widths, labels)
參數(shù):
- x: 包含數(shù)值變量的數(shù)組(一個(gè)箱體),或包含多個(gè)向量的數(shù)組(多個(gè)箱體)
- notch: True -> 鋸齒形箱體,False -> 矩形箱體
- vert: True -> 垂直箱體,False -> 水平箱體
- patch_artist: True -> 用Patch Artist而不是Line2D創(chuàng)建箱體,前者可以高度定制箱體的樣式
- whis: 浮點(diǎn)值或包含兩個(gè)浮點(diǎn)值的元組,默認(rèn)為1.5,控制須線的位置
- 浮點(diǎn)值,箱體下方的須線 = Q1?whis?(Q3?Q1)Q1 - whis*(Q3-Q1)Q1?whis?(Q3?Q1),箱體上方的須線 = Q3+whis?(Q3?Q1)Q3 + whis*(Q3-Q1)Q3+whis?(Q3?Q1),Q1和Q3分別是25%分位數(shù)和75%分位數(shù)
- 包含浮點(diǎn)值的元組,(5.0, 95.0)表示須線覆蓋5%分位數(shù)到95%分位數(shù),(0, 100)表示覆蓋最小值到最大值
- widths: 浮點(diǎn)值或浮點(diǎn)值數(shù)組,設(shè)置箱體的寬度,默認(rèn)為0.5
- labels: 每個(gè)箱體的標(biāo)簽,長(zhǎng)度和x相同
一般情況下,箱線圖用于對(duì)比多個(gè)數(shù)值變量的分布。
np.random.seed(123)arr_1 = np.random.normal(100, 10, 200) arr_2 = np.random.normal(90, 20, 200) arr_3 = np.random.normal(80, 30, 200) arr_4 = np.random.normal(70, 40, 200) arrs = [arr_1, arr_2, arr_3, arr_4]fig, ax = plt.subplots(figsize=(10, 7)) bp = ax.boxplot(arrs, showmeans=True) # 設(shè)置showmeans=True可以添加均值到箱體中 ax.set_title("Comparison of Distributions for multiple variables", fontsize=15)2. 調(diào)整箱線圖的樣式
boxplot()返回一個(gè)包含所有幾何圖形對(duì)象的字典,通過(guò)調(diào)用相應(yīng)對(duì)象的set方法,可以高度定制箱線圖的樣式。
# 查看boxplot()返回什么對(duì)象 bp {'whiskers': [<matplotlib.lines.Line2D at 0x7f9b379aed60>,<matplotlib.lines.Line2D at 0x7f9b379ba100>,<matplotlib.lines.Line2D at 0x7f9b379c5910>,<matplotlib.lines.Line2D at 0x7f9b379c5c70>,<matplotlib.lines.Line2D at 0x7f9b379dc490>,<matplotlib.lines.Line2D at 0x7f9b379dc7f0>,<matplotlib.lines.Line2D at 0x7f9b37967fd0>,<matplotlib.lines.Line2D at 0x7f9b37972370>],'caps': [<matplotlib.lines.Line2D at 0x7f9b379ba460>,<matplotlib.lines.Line2D at 0x7f9b379ba7c0>,<matplotlib.lines.Line2D at 0x7f9b379c5fd0>,<matplotlib.lines.Line2D at 0x7f9b379cf370>,<matplotlib.lines.Line2D at 0x7f9b379dcb50>,<matplotlib.lines.Line2D at 0x7f9b379dceb0>,<matplotlib.lines.Line2D at 0x7f9b379726d0>,<matplotlib.lines.Line2D at 0x7f9b37972a30>],'boxes': [<matplotlib.lines.Line2D at 0x7f9b379aea00>,<matplotlib.lines.Line2D at 0x7f9b379c55b0>,<matplotlib.lines.Line2D at 0x7f9b379dc130>,<matplotlib.lines.Line2D at 0x7f9b37967c70>],'medians': [<matplotlib.lines.Line2D at 0x7f9b379bab50>,<matplotlib.lines.Line2D at 0x7f9b379cf6d0>,<matplotlib.lines.Line2D at 0x7f9b37967250>,<matplotlib.lines.Line2D at 0x7f9b37972d90>],'fliers': [<matplotlib.lines.Line2D at 0x7f9b379c51f0>,<matplotlib.lines.Line2D at 0x7f9b379cfd30>,<matplotlib.lines.Line2D at 0x7f9b379678b0>,<matplotlib.lines.Line2D at 0x7f9b3797d430>],'means': [<matplotlib.lines.Line2D at 0x7f9b379bae50>,<matplotlib.lines.Line2D at 0x7f9b379cf9d0>,<matplotlib.lines.Line2D at 0x7f9b37967550>,<matplotlib.lines.Line2D at 0x7f9b3797d0d0>]} np.random.seed(123)arr_1 = np.random.normal(100, 10, 200) arr_2 = np.random.normal(90, 20, 200) arr_3 = np.random.normal(80, 30, 200) arr_4 = np.random.normal(70, 40, 200) arrs = [arr_1, arr_2, arr_3, arr_4]fig, ax = plt.subplots(figsize=(10, 7))# 創(chuàng)建箱線圖 bp = ax.boxplot(x=arrs,patch_artist=True, # 用Patch Artist而不是Line2D Artist創(chuàng)建箱體notch=True, # 鋸齒形箱體vert=False # 水平箱線圖 )# 設(shè)置箱體顏色 # boxplot不提供color參數(shù),要設(shè)置箱體顏色,首先要從boxplot()返回的結(jié)果中 # 獲取Patch Artists對(duì)象,然后再調(diào)用set_color() colors = ["red", "yellow", "green", "gray"] for patch, color in zip(bp["boxes"], colors):# set_facecolor: 設(shè)置箱體內(nèi)部顏色# set_edgecolor: 設(shè)置箱體邊框顏色# set_color: 設(shè)置箱體內(nèi)部和邊框的顏色patch.set_facecolor(color)# 設(shè)置須線的形狀,大小,顏色 for whisker in bp["whiskers"]:whisker.set(color="red",linestyle="dashed",linewidth=1.5)# 設(shè)置須線帽檐(caps)的樣式 for cap in bp["caps"]:cap.set(color="blue",linewidth=2.5)# 設(shè)置中位數(shù)的樣式 for median in bp["medians"]:median.set(color="black",linewidth=2.0)# 設(shè)置極值點(diǎn)的樣式,超出帽檐的點(diǎn)就是極值點(diǎn) for flier in bp["fliers"]:flier.set(marker="o",markersize=8,markerfacecolor="red",alpha=0.5)# 設(shè)置y軸標(biāo)簽和標(biāo)題 ax.set_yticklabels(["A", "B", "C", "D"]) ax.set_title("Custom Boxplot", fontsize=15)
如果喜歡我們的文章,記得點(diǎn)贊和收藏哦,我們每天都會(huì)為大家?guī)?lái)Python,數(shù)據(jù)科學(xué)和量化交易的精品內(nèi)容。
【關(guān)于我們】
蜂鳥數(shù)據(jù):國(guó)內(nèi)領(lǐng)先的金融數(shù)據(jù)API提供商。
蜂鳥數(shù)據(jù)團(tuán)隊(duì)由業(yè)界頂尖的數(shù)據(jù)工程師,數(shù)據(jù)科學(xué)家和寬客組成,我們正努力構(gòu)建一個(gè)強(qiáng)大的金融數(shù)據(jù)庫(kù),并提供API接口,目標(biāo)是令金融數(shù)據(jù)開源化和平民化。
瀏覽并測(cè)試我們接口吧,目前覆蓋股票,外匯,商品期貨,數(shù)字貨幣和宏觀經(jīng)濟(jì)領(lǐng)域,包括實(shí)時(shí)報(bào)價(jià)(tick)和歷史數(shù)據(jù)(分鐘),提供REST API和Websocket兩種接入方式,能夠滿足金融分析師,量化交易和理財(cái)app的需求。
需要金融數(shù)據(jù)?利用蜂鳥API將數(shù)據(jù)整合到您的應(yīng)用
如果您準(zhǔn)備好了,請(qǐng)登錄蜂鳥官網(wǎng),注冊(cè)免費(fèi)獲取API密鑰,然后開始探索我們的金融數(shù)據(jù)庫(kù)吧。
總結(jié)
以上是生活随笔為你收集整理的Python数据可视化:如何创建箱线图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用阿里云的OSS图片上传,这里是用的上
- 下一篇: 易语言python代码长短_怎样用易语言