2017-10-29 73 views
1

csv文件我有一個很大的csv文件,這裏是一些數據,從它:導入和繪製與Python

"C1_E1",,,,, 
"Time","Force","Disp.","Stress","Strain","Stroke" 
"sec","N","mm","MPa","%","mm" 
"0","0.1192093","0","0.003017193","0","0" 
"0.01","0.09536743","2.083333E-05","0.002413754","1.621271E-05","2.083333E-05" 
"0.02","0.09536743","0.00025","0.002413754","0.0001945525","0.00025" 
"0.03","0.09536743","0.0013125","0.002413754","0.001021401","0.0013125" 
"0.04","0.09536743","0.003604167","0.002413754","0.002804799","0.003604167" 
"0.05","0.09536743","0.006875","0.002413754","0.005350194","0.006875" 
"0.06","0.09536743","0.01104167","0.002413754","0.008592736","0.01104167" 
"0.07","0.09536743","0.01602083","0.002413754","0.01246757","0.01602083" 
"0.08","0.09536743","0.02191667","0.002413754","0.01705577","0.02191667" 
"0.09","0.09536743","0.028625","0.002413754","0.02227626","0.028625" 
"0.1","0.09536743","0.035875","0.002413754","0.02791829","0.035875" 
"0.11","3.910065","0.04352083","0.09896392","0.03386835","0.04352083" 
"0.12","13.39118","0.05145833","0.3389313","0.0400454","0.05145833" 
"0.13","18.46155","0.05989583","0.4672626","0.04661154","0.05989583" 
"0.14","23.57165","0.06875","0.5965995","0.05350194","0.06875" 

我試圖使用numpy的繪製應力和應變列和matplotlib等等我寫了下面的代碼:

from numpy import * 
import matplotlib.pyplot as plt 

stress_data = genfromtxt('C1-E1.csv', delimiter=',', skip_header=3, usecols=([3])) 
strain_data = genfromtxt('C1-E1.csv', delimiter=',', skip_header=3, usecols=([4])) 
print(stress_data[:10], strain_data[:10]) 

map(float, stress_data) 
map(float, strain_data) 

print(stress_data[:10], strain_data[:10]) 

plt.xlabel ('x stuff') 
plt.ylabel ('y stuff') 
plt.title('my test result') 
plt.plot(strain_data[:10], stress_data[:10]) 
plt.show() 

的問題是,輸出

[ nan nan nan nan nan nan nan nan nan nan] [ nan nan nan nan nan nan nan nan nan nan] 
[ nan nan nan nan nan nan nan nan nan nan] [ nan nan nan nan nan nan nan nan nan nan] 

,當然沒有情節而成。我敢肯定有在genfromtxt一個問題,因爲如果我設置D型=(「| S20」),然後我得到

[b'"0.003017193"' b'"0.002413754"' b'"0.002413754"' b'"0.002413754"' 
b'"0.002413754"' b'"0.002413754"' b'"0.002413754"' b'"0.002413754"' 
b'"0.002413754"' b'"0.002413754"'] [b'"0"' b'"1.621271E-05"' b'"0.0001945525"' b'"0.001021401"' 
b'"0.002804799"' b'"0.005350194"' b'"0.008592736"' b'"0.01246757"' 
b'"0.01705577"' b'"0.02227626"'] 

這打亂了由於1.621271E-05科學記數法是劇情。有沒有一種方法可以從csv文件中提取數據並將其轉換爲可處理科學記數法的格式,以便我可以繪製和分析它?

對不起,很長的文章,但我不知道該去哪裏。

回答

1

當你想在csv文件上工作時,熊貓庫是非常有用的。

這裏,讓一切更簡單:

import pandas as pd 
import matplotlib.pyplot as plt 

df = pd.read_csv('data_1.csv',header=1) 

df =df[1:].astype(float) # Deletes the first row as it contains the unit and is not usefull if you want to plot the data, and convert the dataframe type to float 

繪製力作爲時間的函數,你就必須做到以下幾點:

plt.plot(df['Time'],df['Force']) 
+0

這是一個完美的執行dataframes。幹得不錯! – gommb