亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python繪制二維曲線的日常應(yīng)用詳解

 更新時間:2019年12月04日 10:34:36   作者:grey_csdn  
今天小編就為大家分享一篇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)文章

最新評論