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ù),計算自然對數(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. vue實(shí)現(xiàn)web在線聊天功能2. 完美解決vue 中多個echarts圖表自適應(yīng)的問題3. JavaScript實(shí)現(xiàn)頁面動態(tài)驗證碼的實(shí)現(xiàn)示例4. 解決Android Studio 格式化 Format代碼快捷鍵問題5. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis6. Java使用Tesseract-Ocr識別數(shù)字7. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼8. 在Chrome DevTools中調(diào)試JavaScript的實(shí)現(xiàn)9. Springboot 全局日期格式化處理的實(shí)現(xiàn)10. SpringBoot+TestNG單元測試的實(shí)現(xiàn)
