溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-05-19 10:58:03 來源:億速云 閱讀:163 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Python如何繪制二維曲線的日常應(yīng)用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

使用Python繪制出類似Excel或者M(jìn)ATLAB的曲線還是比較容易就能夠?qū)崿F(xiàn)的,需要用到的額外庫有兩個(gè),numpy和matplotlib。使用這兩個(gè)模塊實(shí)現(xiàn)的曲線繪制其實(shí)在一定程度上更像是MATLAB的plot功能,不過今天看了一下matplotlib網(wǎng)站上的信息,現(xiàn)在的功能更為強(qiáng)勁了,而且已經(jīng)支持三維圖像的繪制。

模塊庫的安裝非常簡(jiǎn)單,我使用的Mac,在Mac上用pip進(jìn)行了兩個(gè)模塊庫的安裝都十分順暢。相信其他平臺(tái)基本上也都這樣,如果能夠聯(lián)網(wǎng),這種安裝方式是十分推薦的,確實(shí)是簡(jiǎn)單。

我用Python讀取我自己日常運(yùn)動(dòng)的數(shù)據(jù),數(shù)據(jù)以Numbers的方式進(jìn)行統(tǒng)計(jì),導(dǎo)出成Excel文件。為了能夠讀取Excel文件,我又安裝了xlrd模塊庫。

從matplotlib的網(wǎng)站上抄了一小段代碼簡(jiǎn)單做了一下修改,加入了數(shù)據(jù)讀取以及簡(jiǎn)單的計(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')

我的運(yùn)動(dòng)數(shù)據(jù)記錄電子表格格式如下:

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

程序運(yùn)行,畫出的曲線如下:

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

python有哪些常用庫

python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

感謝各位的閱讀!關(guān)于“Python如何繪制二維曲線的日常應(yīng)用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI