Python實(shí)例3—從Excel中調(diào)用數(shù)據(jù)并繪制成圖

Python實(shí)例3—從Excel中調(diào)用數(shù)據(jù)并繪制成圖的圖1

Excel是一個(gè)對(duì)處理數(shù)據(jù)很友好的軟化,大量的數(shù)據(jù)可以在其中處理。筆者今天學(xué)習(xí)了一下使用Python從Excel中調(diào)用數(shù)據(jù)并繪制成圖的方法,將該方法分享給大家。

案例:

我們?cè)趀xcel第四個(gè)“sheet”表格中的第一列和第二列輸入一組滯回曲線的數(shù)據(jù)(excel數(shù)據(jù)及圖如下圖所示),那么怎么使用python進(jìn)行調(diào)用而讀取excel中的數(shù)據(jù)呢?

Python實(shí)例3—從Excel中調(diào)用數(shù)據(jù)并繪制成圖的圖2

  • 調(diào)用python讀取excel數(shù)據(jù)方法

代碼如下:

import xlrd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
t = xlrd.open_workbook('柱子實(shí)驗(yàn)數(shù)據(jù)(2).xls')
sheet = t.sheet_by_index(3)
x_data=[] #需要將數(shù)據(jù)儲(chǔ)存在空列表中才可調(diào)用繪圖
y_data=[]
for row in range(sheet.nrows):  
  content1 = sheet.cell_value(row,0)  
  x_data.append(content1)  
  content2 = sheet.cell_value(row,1)  
  y_data.append(content2)
 plt.plot(x_data,y_data,'r-')
 plt.xlabel('位移')
 plt.ylabel('荷載')
 plt.title('滯回曲線')
 plt.show()
注意:我們使用命令xlrd.open_workbook進(jìn)行調(diào)用excel時(shí),其中的路徑可輸入相對(duì)路徑或絕對(duì)路徑,也就是excel是否和py文件放在同一個(gè)文件夾下面。

其調(diào)用結(jié)果如下圖所示:

Python實(shí)例3—從Excel中調(diào)用數(shù)據(jù)并繪制成圖的圖3


今天就先講這么多吧,下次推文寫一下如何調(diào)用python在excel中寫入數(shù)據(jù)的方法。希望代碼能夠?qū)Ω魑蛔x者有所幫助!!!

歡迎關(guān)注公眾號(hào)“土木愛研小站”并加入學(xué)術(shù)交流群


您的每一個(gè)贊和關(guān)注都是我前進(jìn)的動(dòng)力!!!Python實(shí)例3—從Excel中調(diào)用數(shù)據(jù)并繪制成圖的圖4Python實(shí)例3—從Excel中調(diào)用數(shù)據(jù)并繪制成圖的圖5Python實(shí)例3—從Excel中調(diào)用數(shù)據(jù)并繪制成圖的圖6

登錄后免費(fèi)查看全文
立即登錄
App下載
技術(shù)鄰APP
工程師必備
  • 項(xiàng)目客服
  • 培訓(xùn)客服
  • 平臺(tái)客服

TOP