我有一个很大的csv文件,这里有一些数据:
"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中存在问题,因为如果我设置dtype =(" | 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文件中提取数据并将其转换为可以处理科学记数法的格式,以便我可以绘制和分析它?
很抱歉很长的帖子,但我不知道该去哪里。
答案 0 :(得分:1)
如果你想处理csv文件,pandas库真的很有用。
在这里,它使一切变得更加简单:
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'])