用python 制作直方圖

利用jupyter notebook 軟件制作

一、導(dǎo)入文件

方法1:

  1. 打開電腦命令符,直接輸入jupyter notebook
  2. 電腦在瀏覽器下直接打開 jupyter notebook界面
  3. 點擊upload, 找到需要導(dǎo)入的文件,導(dǎo)入到文件目錄
  4. 選擇此文件,點擊new, 創(chuàng)建notebook

方法2:

  1. 打開電腦命令符,輸入cd +空格+ 要導(dǎo)入文件的地址(cd : change directory)
  2. 在此文件下打開jupyter notebook
  3. 可以看到直接打開此文件目錄下
  4. 選擇此文件,點擊new, 創(chuàng)建notebook

二、python 計算

*# 導(dǎo)入numpy庫, 命名為np *
import numpy as np
import matplotlib.pyplt as plt
import pandas as pd

#用panda來讀取‘weight.txt’文件的數(shù)據(jù), 函數(shù)read_table
weight_data = pd.read_table(‘weight.txt’)

weight_data.shape()

#有一列數(shù),80個
(80,1)


weight_data['weight']mean()

50.7


weight_data[weight].var()

weight 39.275949


weight_data[weight].median()

weight 50.0


制作直方圖

*設(shè)置圖標尺寸和DPI, 設(shè)置600 * 300 像素,每英寸100像素*
fig = plt.figure(figsize = (6,3) ,dpi = 100)*

x = weight_data['weight']

# 將畫布分割成1行1列,從左到右從上到下第1塊
ax= fig.add_subplot(111)

#bins-直方個數(shù),alpha-顏色的深淺度,rwidth-寬度,normed - 是否對數(shù)據(jù)標準化
ax.hist(x,bins = 15, color = 'red',alpha = 0.5,rwidth = 0.8,normed=False)

plt.grid(True)
plt.title(u'weight')
plt.show()


weight 直方圖,數(shù)據(jù)波動較大

csv文件

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
passengers_data = pd.read_csv('AirPassengers.csv')
passengers_data.shape

(144,2)


passengers_data['NumPassengers'].mean()

280.2986111111111


passengers_data['NumPassengers'].var()

14391.917200854701


passengers_data['NumPassengers'].median()

265.5


fig = plt.figure(figsize = (6, 3), dpi = 100)
x = passengers_data['NumPassengers']
ax = fig.add_subplot(111)
ax.hist(x , bins = 50, color = 'red', alpha = 0.5, rwidth = 0.8, normed = False)
plt.grid(True)
plt.title('passenger')
plt.show()


passenger,數(shù)據(jù)波動較大

參考文檔:
文檔 matplotlib繪圖總結(jié);
文檔 Python數(shù)據(jù)可視化分析 matplotlib教程;
文檔PANDAS常用手冊 I --讀寫文本數(shù)據(jù) ;
文檔 matplotlib.pyplot中add_subplot方法參數(shù)111的含義 ;
文檔 Matplotlib使用教程;
書籍:《極簡統(tǒng)計學(xué)》

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容