根據(jù)箱型圖、直方圖的代碼和數(shù)據(jù)的條件查詢方法,畫出航空公司男性和女性用戶的年齡分布箱型圖和直方圖。
目錄
?圖形簡介
1. 箱線圖
2.直方圖
引入模塊
獲取數(shù)據(jù)
處理數(shù)據(jù)
根據(jù)性別來分開查詢數(shù)據(jù)
畫圖
箱型圖
?直方圖
男性直方圖
?圖形簡介
1. 箱線圖
箱線圖(Box-plot)又稱為盒式圖或箱型圖,箱型圖是用來表示一組數(shù)據(jù)的分布(統(tǒng)計不同取值可能出現(xiàn)的次數(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.直方圖
直方圖(Bar),形狀類似柱狀圖卻有著與柱狀圖完全不同的含義。直方圖牽涉統(tǒng)計學(xué)概念,首先要對數(shù)據(jù)進(jìn)行分組,然后統(tǒng)計每個分組內(nèi)數(shù)據(jù)元的數(shù)量。在平面直角坐標(biāo)系中,橫軸標(biāo)出每個組的端點,縱軸表示頻數(shù),每個矩形的高代表對應(yīng)的頻數(shù),這樣的統(tǒng)計圖稱為頻數(shù)分布直方圖。
頻數(shù)分布直方圖需要經(jīng)過頻數(shù)乘以組距的計算過程才能得出每個分組的數(shù)量,同一個直方圖的組距是一個固定不變的值,所以如果直接用縱軸表示數(shù)量,每個矩形的高代表對應(yīng)的數(shù)據(jù)元數(shù)量,既能保持分布狀態(tài)不變,又能直觀地看出每個分組的數(shù)量,如下圖所示
引入模塊
首先我們的目的是獲取excel文件并作圖,那么就要引入相關(guān)的模塊
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Boxplot
from pyecharts.charts import Bar
若沒有pandas和pyecharts這兩個模塊!
就需要在cmd命令窗口或Anaconda Prompt窗口輸入以下命令:
pip install pandas
pip install pyecharts
---------------------------------------------------------------------------------------------------------------------------------
Pandas 是 Python 語言的一個擴(kuò)展程序庫,用于數(shù)據(jù)分析。
pyecharts是一款將python與echarts結(jié)合的強(qiáng)大的數(shù)據(jù)可視化工具。Echarts 是百度開源的一個數(shù)據(jù)可視化 JS 庫, Echarts可以生成非常棒的可視化交互圖,pyecharts的開發(fā)讓在python平臺上也可以直接使用數(shù)據(jù)生成圖。
獲取數(shù)據(jù)
利用pandas中的方法獲取指定文件指定工作表中的全部數(shù)據(jù),其數(shù)據(jù)是一個列表形式,注意這里’航空公司數(shù)據(jù)-剔除空年齡.xlsx‘文件與當(dāng)前寫的xx.ipynb文件需要在同一文件夾內(nèi)才能獲取到數(shù)據(jù),因為我使用的是相對路徑,也可以使用絕對路徑來獲取‘航空公司數(shù)據(jù)-剔除空年齡.xlsx’文件。
下面是用相對路徑來獲取。
data = pd.read_excel(r'航空公司數(shù)據(jù)-剔除空年齡.xlsx')
data
?或者用絕對路徑來獲取。
data = pd.read_excel(r'D:\新建文件夾 (2)\航空公司數(shù)據(jù)-剔除空年齡.xlsx')
data
處理數(shù)據(jù)
根據(jù)性別來分開查詢數(shù)據(jù)
表格對象中實現(xiàn)條件查詢的方法:
data1 = data['性別']=="男"
data2 = data['性別']=="女"
data11=data.loc[data1,:]
data11
data22=data.loc[data2,:]
data22
做完以上步驟再把年齡提取出來
y1 = [data11['年齡'].tolist()]
y2 = [data22['年齡'].tolist()]
畫圖
箱型圖
畫圖前先把年齡的最小值,下四分位數(shù),中位數(shù),上四分位數(shù),最大值提取出來
男性年齡
c = Boxplot()
c.prepare_data(y1)
?
女性年齡?
c = Boxplot()
c.prepare_data(y2)
完成以上步驟開始畫圖
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='skyblue',
border_color='blue',
)
)
c.add_yaxis("女",
c.prepare_data(y2),
itemstyle_opts = opts.ItemStyleOpts(color='#aa1010',
border_color='black',
)
)
c.set_global_opts(title_opts=opts.TitleOpts(title="男女性年齡分布箱型圖"),
yaxis_opts=opts.AxisOpts(
type_="value",
name="年齡", # 單位
splitarea_opts=opts.SplitAreaOpts(
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
),
)
c.render_notebook()
直方圖
畫圖前先把每個年齡段有多少人提取出來
男性直方圖
dist = data11['年齡'].value_counts().sort_index()
dist
?
x = [i for i in dist.index]
y = dist.tolist()
c = (
Bar()
.add_xaxis(x)
.add_yaxis("男", y,
category_gap=0, # 設(shè)置柱子之間的間距為0
color='#ff8080')
.set_global_opts(title_opts=opts.TitleOpts(title="男性直方圖"),
xaxis_opts=opts.AxisOpts(
name="年齡", # 單位
),
yaxis_opts=opts.AxisOpts(
type_="value",
name="人數(shù)", # 單位
splitarea_opts=opts.SplitAreaOpts(
is_show=True,areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
),
)
)
c.render_notebook()
女性直方圖
dict = data22['年齡'].value_counts().sort_index()
dict
?文章來源:http://www.zghlxwxcb.cn/news/detail-751722.html
x = [i for i in dict.index]
y = dict.tolist()
c = (
Bar()
.add_xaxis(x)
.add_yaxis("女", y,
category_gap=0, # 設(shè)置柱子之間的間距為0
color='#ff8080')
.set_global_opts(title_opts=opts.TitleOpts(title="女性直方圖"),
xaxis_opts=opts.AxisOpts(
name="年齡", # 單位
),
yaxis_opts=opts.AxisOpts(
type_="value",
name="人數(shù)", # 單位
splitarea_opts=opts.SplitAreaOpts(
is_show=True,areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
),
)
)
c.render_notebook()
文章來源地址http://www.zghlxwxcb.cn/news/detail-751722.html
到了這里,關(guān)于Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!