Python繪制二維曲線的日常應(yīng)用詳解
使用Python繪制出類似Excel或者MATLAB的曲線還是比較容易就能夠?qū)崿F(xiàn)的,需要用到的額外庫有兩個,numpy和matplotlib。使用這兩個模塊實現(xiàn)的曲線繪制其實在一定程度上更像是MATLAB的plot功能,不過今天看了一下matplotlib網(wǎng)站上的信息,現(xiàn)在的功能更為強勁了,而且已經(jīng)支持三維圖像的繪制。
模塊庫的安裝非常簡單,我使用的Mac,在Mac上用pip進行了兩個模塊庫的安裝都十分順暢。相信其他平臺基本上也都這樣,如果能夠聯(lián)網(wǎng),這種安裝方式是十分推薦的,確實是簡單。
我用Python讀取我自己日常運動的數(shù)據(jù),數(shù)據(jù)以Numbers的方式進行統(tǒng)計,導(dǎo)出成Excel文件。為了能夠讀取Excel文件,我又安裝了xlrd模塊庫。
從matplotlib的網(wǎng)站上抄了一小段代碼簡單做了一下修改,加入了數(shù)據(jù)讀取以及簡單的計算,代碼如下:
#!/usr/bin/python import numpy as np import matplotlib.pyplot as plt from xlrd import open_workbook def SportLine(excel_file): days_year = [] target_km = [] records = [] sum_records = [] pct_records = [] target_pct = [] fig,axs = plt.subplots(3) for i in range(365): days_year.append(i) for day in days_year: target_km.append(float(day)/365.0 * 1000.0) # read record data book = open_workbook(excel_file) sheet = book.sheet_by_name('record') rows_num = sheet.nrows cols_num = sheet.ncols for row_num in range(3,368): try: records.append(float(sheet.cell(row_num,1).value)) except: records.append(0.0) # calculate sum of records sum_record = 0.0 for each_record in records: sum_record += each_record sum_records.append(sum_record) # calculate pct of all for each_sum in sum_records: pct_records.append(each_sum / 1000.0) # calculate target pct for day in range(1,366): target_pct.append(float(day)/365.0) # plot target and sum trend ax = axs[0] ax.plot(days_year,sum_records) ax.plot(days_year,target_km) ax.set_title('distance-year-km') ax.grid(True) # plot record ax = axs[1] ax.plot(days_year,records) ax.set_title('distance-day-km') ax.grid(True) # plot percentage ax = axs[2] ax.plot(days_year,pct_records) ax.plot(days_year,target_pct) ax.set_title('pct-100%') ax.grid(True) plt.show() SportLine('records.xlsx')
我的運動數(shù)據(jù)記錄電子表格格式如下:
程序運行,畫出的曲線如下:
基本差不多了,后面需要做的只有細節(jié)上的修正了。
以上這篇Python繪制二維曲線的日常應(yīng)用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python smtplib模塊自動收發(fā)郵件功能(一)
這篇文章主要為大家詳細介紹了python smtplib模塊自動收發(fā)郵件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Python使用pyodbc訪問數(shù)據(jù)庫操作方法詳解
這篇文章主要介紹了Python使用pyodbc訪問數(shù)據(jù)庫操作方法,結(jié)合實例形式詳細分析了Python基于pyodbc針對數(shù)據(jù)庫的連接、查詢、插入、修改、刪除等操作技巧與注意事項,需要的朋友可以參考下2018-07-07numpy:np.newaxis 實現(xiàn)將行向量轉(zhuǎn)換成列向量
今天小編就為大家分享一篇numpy:np.newaxis 實現(xiàn)將行向量轉(zhuǎn)換成列向量,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11pandas DataFrame實現(xiàn)幾列數(shù)據(jù)合并成為新的一列方法
今天小編就為大家分享一篇pandas DataFrame實現(xiàn)幾列數(shù)據(jù)合并成為新的一列方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06TensorFlow卷積神經(jīng)網(wǎng)絡(luò)AlexNet實現(xiàn)示例詳解
這篇文章主要為大家介紹了TensorFlow卷積神經(jīng)網(wǎng)絡(luò)AlexNet實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11