新聞中心
這篇文章給大家分享的是有關(guān)Python如何繪制二維曲線的日常應(yīng)用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
使用Python繪制出類似Excel或者M(jìn)ATLAB的曲線還是比較容易就能夠?qū)崿F(xiàn)的,需要用到的額外庫(kù)有兩個(gè),numpy和matplotlib。使用這兩個(gè)模塊實(shí)現(xiàn)的曲線繪制其實(shí)在一定程度上更像是MATLAB的plot功能,不過(guò)今天看了一下matplotlib網(wǎng)站上的信息,現(xiàn)在的功能更為強(qiáng)勁了,而且已經(jīng)支持三維圖像的繪制。
模塊庫(kù)的安裝非常簡(jiǎn)單,我使用的Mac,在Mac上用pip進(jìn)行了兩個(gè)模塊庫(kù)的安裝都十分順暢。相信其他平臺(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模塊庫(kù)。
從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ù)記錄電子表格格式如下:
程序運(yùn)行,畫出的曲線如下:
python有哪些常用庫(kù)
python常用的庫(kù):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ò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享名稱:Python如何繪制二維曲線的日常應(yīng)用-創(chuàng)新互聯(lián)
本文鏈接:http://www.dlmjj.cn/article/dgpehj.html