日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python数据分析】四级成绩分布 -matplotlib,xlrd 应用

發(fā)布時間:2025/3/15 python 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python数据分析】四级成绩分布 -matplotlib,xlrd 应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近獲得了一些四級成績數(shù)據(jù),大概500多個,于是突發(fā)奇想是否能夠看看這些成績數(shù)據(jù)是否滿足所謂的正態(tài)分布呢?說干就干,于是有了這篇文章。
文章順帶介紹了xlrd模塊的一些用法和matplotlib畫自定義數(shù)據(jù)的條形圖和隨機的條形圖的一些方法,并且提供了一些相關鏈接,可作為學習matplotlib和numpy的資源,希望對讀者也有幫助。

更優(yōu)美的格式見這里

工具

  • Python 3.5
  • xlrd模塊
  • numpy模塊及一些依賴模塊(安裝請自行查詢方法,絕大部分pip就可搞定)
  • matplotlib繪圖模塊

xlrd基本用法

1、導入模塊

1
import xlrd

2、打開Excel文件讀取數(shù)據(jù)

1
data = xlrd.open_workbook('excelFile.xls')

3、使用技巧

  • 獲取一個工作表

    1
    2
    3
    table = data.sheets()[0] #通過索引順序獲取
    table = data.sheet_by_index(0) #通過索引順序獲取
    table = data.sheet_by_name(u'Sheet1')#通過名稱獲取
  • 獲取整行和整列的值(數(shù)組)

    1
    2
    3
    4
    5
    6
    7
    table.row_values(i)
    table.col_values(i)
    ```
    * 獲取行數(shù)和列數(shù)
    ```python
    nrows = table.nrows
    ncols = table.ncols
  • 循環(huán)行列表數(shù)據(jù)

    1
    2
    for i in range(nrows ):
    print table.row_values(i)
  • 單元格

    1
    2
    cell_A1 = table.cell(0,0).value
    cell_C4 = table.cell(2,3).value
  • 使用行列索引

    1
    2
    cell_A1 = table.row(0)[0].value
    cell_A2 = table.col(1)[0].value
  • 簡單的寫入

    1
    2
    3
    4
    5
    6
    7
    8
    row = 0
    col = 0
    #類型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
    ctype = 1 value = '單元格的值'
    xf = 0 # 擴展的格式化
    table.put_cell(row, col, ctype, value, xf)
    table.cell(0,0) #單元格的值'
    table.cell(0,0).value #單元格的值'

畫折線圖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import xlrd
import numpy as np
import matplotlib.pyplot as plt

data = xlrd.open_workbook('D:\\Python Workspace\\Data\\cet4.xls')

table = data.sheets()[0] #sheet 0

col5 = table.col_values(5)[1:] #取第5列的成績,并去掉列屬性名稱

count = [0 for i in range(0,650)] #初始化count
x = [i for i in range(0,650)]

for i in col5:
num = int(i)
count[num] += 1 #統(tǒng)計每個人數(shù)的人數(shù)

plt.xlabel('Score')
plt.ylabel('Number of people')
plt.title('Distribution of CET-4 Scores')
plt.ylim(0,8)
plt.plot([i for i in range(250,650) if count[i] != 0],[i for i in count[250:] if i != 0],linewidth=1) #畫出折線圖
plt.show()

圖1

畫直方圖并與正態(tài)分布直方圖對比

import xlrd import numpy as np from math import * import pylab as pl import matplotlib.pyplot as pltdata = xlrd.open_workbook('D:\\Python Workspace\\Data\\cet4.xls')table = data.sheets()[0] #sheet 0col5 = table.col_values(5)[1:]ha = [int(i) for i in col5] #成績數(shù)據(jù) mu = np.mean(ha) #平均值 sigma = np.std(ha) #標準差 data = np.random.normal(mu,sigma,1000) #生成正態(tài)分布隨機數(shù)據(jù)x = np.linspace(0,700,1000) y = (1. / sqrt(2 * np.pi) / sigma)*np.exp( -((x-mu)**2/(2*sigma**2)) )plt.hist(data,bins=100,facecolor='g',alpha=0.44) plt.hist(ha,bins=70,facecolor='r',histtype='stepfilled') plt.plot(x,y,color='b') #正態(tài)分布曲線plt.xlabel('Score') plt.ylabel('Number of people') plt.title('Distribution of CET-4 Scores') plt.show()

圖2
且可求得數(shù)據(jù)的均值和標準差分別為:476.743785851和104.816562585
由圖可見,綠色條形圖是$\mu$=476.743785851,$\sigma$=104.816562585的正態(tài)分布條形圖,而紅色是四級成績數(shù)據(jù)的分布圖,雖然由于數(shù)據(jù)較少(500多個數(shù)據(jù)),所以擬合較差,但是可以看出成績數(shù)據(jù)還是基本滿足正態(tài)分布的。
不知道為啥,正態(tài)曲線沒有畫出來,單獨畫正態(tài)曲線是可以畫出來的,有待研究。

繪制直方圖的一些參數(shù)解釋

繪圖都可以調(diào)用matplotlib.pyplot庫來進行,其中的hist函數(shù)可以直接繪制直方圖。

調(diào)用方式:

1
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='black',alpha=1,histtype='bar')

?

hist的參數(shù)非常多,但常用的就這六個,只有第一個是必須的,后面四個可選

arr: 需要計算直方圖的一維數(shù)組

bins: 直方圖的柱數(shù),可選項,默認為10

normed: 是否將得到的直方圖向量歸一化。默認為0

facecolor: 直方圖顏色

edgecolor: 直方圖邊框顏色

alpha: 透明度

histtype: 直方圖類型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’

返回值 :

n: 直方圖向量,是否歸一化由參數(shù)normed設定

bins: 返回各個bin的區(qū)間范圍

patches: 返回每個bin里面包含的數(shù)據(jù),是一個list

摘自這里?from denny

一些鏈接

matplotlib

庫的主頁
gallary

matplotlib的一些示例及其代碼,是很好的學習工具。
用python做科學計算

用Python作科學計算的一些工具
xlrd文檔
numpy的一些方法

轉載于:https://www.cnblogs.com/whatbeg/p/5390464.html

總結

以上是生活随笔為你收集整理的【Python数据分析】四级成绩分布 -matplotlib,xlrd 应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。