本文示例:根據(jù)箱型圖、直方圖的代碼和數(shù)據(jù)的條件查詢方法,畫出航空公司男性和女性用戶的年齡分布箱型圖和直方圖。
目錄
圖形概念
1.箱型圖
2.直方圖
步驟:
1、導(dǎo)入相關(guān)庫
2、對數(shù)據(jù)進行處理
?3、繪制圖形
????????????????箱型圖
????????????????直方圖
圖形概念
1.箱型圖
????????箱型圖是一種用作顯示一組數(shù)據(jù)分散情況資料的統(tǒng)計圖,它主要用于反映原始數(shù)據(jù)分布的特征,還可以進行多組數(shù)據(jù)分布特征的比 較。箱線圖的繪制方法是:先找出一組數(shù)據(jù)的上邊緣、下邊緣、中位數(shù)和兩個四分位數(shù);然后, 連接兩個四分位數(shù)畫出箱體;再將上邊緣和下邊緣與箱體相連接,中位數(shù)在箱體中間。
25%分位數(shù)(下四分位數(shù)):序列中有25%的數(shù)據(jù)小于這個數(shù)
中位數(shù)(50%分位數(shù)):序列中有50%的數(shù)據(jù)小于這個數(shù)
75%分位數(shù)(上四分位數(shù)):序列中有75%的數(shù)據(jù)小于這個數(shù)
最大值:序列100%的數(shù)據(jù)小于這個數(shù)
最小值:序列沒有數(shù)據(jù)小于這個數(shù)
2.直方圖
直方圖(Histogram),又稱質(zhì)量分布圖,是一種統(tǒng)計報告圖,由一系列高度不等的縱向條紋或線段表示數(shù)據(jù)分布的情況。 一般用橫軸表示數(shù)據(jù)類型,縱軸表示分布情況。直方圖容易和柱形圖搞混淆,外觀上沒有區(qū)別,但是直方圖一般指用來描述數(shù)據(jù)的分布的柱形圖。
直方圖的常見作用有以下三點:
(1)顯示質(zhì)量波動的狀態(tài);
(2)較直觀地傳遞有關(guān)過程質(zhì)量狀況的信息;
(3)通過研究質(zhì)量波動狀況之后,就能掌握過程的狀況,從而確定在什么地方集中力量進行質(zhì)量改進工作。
步驟:
1、導(dǎo)入相關(guān)庫
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Boxplot
from pyecharts.charts import Bar
1、Boxplot庫是pyecharts中的一個圖表類型,用于顯示數(shù)據(jù)的統(tǒng)計分布情況。
2、Bar庫是pyecharts中的另外一個圖表類型,用于繪制柱狀圖。Bar圖可以展示不同類別的數(shù)據(jù)大小之間的比較,也可以用于顯示時間序列數(shù)據(jù)的變化。
讀取文件,獲取數(shù)據(jù)集
data = pd.read_excel(r'航空公司數(shù)據(jù)-剔除空年齡.xlsx')
data
2、對數(shù)據(jù)進行處理
篩選出表格中性別為男的存給變量a1,性別為女的存給變量a3
a1 = data['性別']=="男"
a2 = data['性別']=="女"
?把表格中性別為男的數(shù)據(jù)存給變量a3并打印出來
a3 = data.loc[a1,:]
a3
?把表格中性別為女的數(shù)據(jù)存給變量a4并打印出來
a4 = data.loc[a2,:]
a4
把年齡那一列變?yōu)槎S列表
pyecherts 中有特殊的要求,加入的數(shù)據(jù)必須是列表類型的,所以在畫圖之前我先對所需數(shù)據(jù)進行處理,處理的方法有以下幾種(list()函數(shù)、字符串轉(zhuǎn)列表、tolist()等),這里我用的是tolist()函數(shù)。
y1 = [a3['年齡'].tolist()] # 注意需要二維列表
y2 = [a4['年齡'].tolist()]
創(chuàng)建一個箱型圖對象,并加入男性的二維列表數(shù)據(jù)
c = Boxplot()
c.prepare_data(y1) # [最小值,下四分位數(shù),中位數(shù),上四分位數(shù),最大值]
女性的同上
c = Boxplot()
c.prepare_data(y2)
?3、繪制圖形
箱型圖
# from pyecharts import options as opts
# from pyecharts.charts import Boxplot
c = Boxplot() # 先創(chuàng)建出圖形對象
c.add_xaxis([""]) # 必須輸入,但多數(shù)時候不需要
c.add_yaxis("男",
c.prepare_data(y1), # 數(shù)據(jù)經(jīng)過prepare_data方法處理得到[最小值,下四分位數(shù),中位數(shù),上四分位數(shù),最大值]
itemstyle_opts = opts.ItemStyleOpts(color='#FFDAB9',
border_color='#F08080',
)
)
c.add_yaxis("女",
c.prepare_data(y2),
itemstyle_opts = opts.ItemStyleOpts(color='#87CEEB', #配置箱子顏色
border_color='blue', #箱子的邊框顏色
)
)
# 全局配置項
c.set_global_opts(title_opts=opts.TitleOpts(title="男女性年齡分布箱型圖"), #標題
yaxis_opts=opts.AxisOpts( # y軸的標題
type_="value", # 類型
name="年齡", # y軸標簽
splitarea_opts=opts.SplitAreaOpts( # 設(shè)置標簽不顯示
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
),
)
c.render_notebook()
結(jié)果展示:
這組數(shù)據(jù)顯示出:
男性? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 女性
-
最小值(minimum)=13? ? ? ? ? ? ? ? ? ? ? ? ? ? 最小值(minimum)=12
-
下四分位數(shù)(Q1)=38? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?下四分位數(shù)(Q1)=35
-
中位數(shù)(Med--也就是Q2)=44? ? ? ? ? ? ? ? ? ?中位數(shù)(Med--也就是Q2)=40
-
上四分位數(shù)(Q3)=50? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 上四分位數(shù)(Q3)=48
-
最大值(maximum)=89? ? ? ? ? ? ? ? ? ? ? ? ? ? ?最大值(maximum)=78
直方圖
畫圖前分組并把篩選出來的數(shù)據(jù)轉(zhuǎn)為列表
dict = data.groupby(by=['性別','年齡'])['年齡'].count() #分組
x1 = dict['女'].index.tolist() #把篩選出來的數(shù)據(jù)轉(zhuǎn)為列表
x11 = dict['女'].values.tolist()
x2 = dict['男'].index.tolist()
x22 = dict['男'].values.tolist()
繪制女性直方圖
x = [i for i in dict.index]
y = dict.tolist()
c = (
Bar()
.add_xaxis(x1)
.add_yaxis("女", x11,
category_gap=0, # 設(shè)置柱子之間的間距為0
color='#87CEEB')
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-直方圖"))
)
c.render_notebook()
?繪制男性直方圖文章來源:http://www.zghlxwxcb.cn/news/detail-671407.html
x = [i for i in dict.index]
y = dict.tolist()
c = (
Bar()
.add_xaxis(x2)
.add_yaxis("男", x22,
category_gap=0, # 設(shè)置柱子之間的間距為0
color='#ff8080')
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-直方圖"))
)
c.render_notebook()
文章來源地址http://www.zghlxwxcb.cn/news/detail-671407.html
到了這里,關(guān)于Python數(shù)據(jù)分布類型圖(箱型圖、直方圖)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!