Python使用Numpy模塊讀取文件并繪制圖片
代碼如下
import pandas as pdimport matplotlib.pyplot as pltimport numpy as npdata = np.loadtxt(’distance.txt’,dtype = np.int)print(data)x = data[:,0] # 設(shè)置第1列數(shù)據(jù)為x軸數(shù)據(jù)。y = np.log(data[:,1]) # 設(shè)置第2列為y軸數(shù)據(jù),計(jì)算自然對數(shù)后賦值給y, 注意如果取以10為底的對數(shù),則需要使用log10方法。print(x[2])print(y[2])plt.figure(figsize= (6, 2.5)) # 設(shè)置圖形寬高比plt.plot(x, y, ’o’)# plt.ylabel(’log(PGV)(微米/秒’)plt.plot(x[2],y[2],’o’,color=’red’) # 用第3行數(shù)據(jù)突出顯示數(shù)據(jù),紅線圓點(diǎn)。plt.grid(ls = ’--’) # 設(shè)置網(wǎng)絡(luò)線風(fēng)格為虛線plt.show()
結(jié)果

使用Numpy模塊的loadtxt方法讀取數(shù)據(jù)為數(shù)組,這種讀取文件的方法比通常的open方式讀取的文件,更容易操作。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 什么是python的自省2. php模擬實(shí)現(xiàn)斗地主發(fā)牌3. 詳解Android studio 動態(tài)fragment的用法4. spring acegi security 1.0.0 發(fā)布5. MyBatis中的JdbcType映射使用詳解6. vue 使用localstorage實(shí)現(xiàn)面包屑的操作7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. Vuex localStorage的具體使用9. Python random庫使用方法及異常處理方案10. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼

網(wǎng)公網(wǎng)安備