1

I have a big csv file, here is some of the data from it:

"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"

I'm trying to plot the stress and strain columns using numpy and matplotlib so I wrote the following code:

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()

The problem is that the output is

[ 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]

and of course no plot is made. I'm sure there is a problem in genfromtxt because if I set dtype=("|S20") then I get

[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"']

which messes up the plot due to 1.621271E-05 being in scientific notation. Is there a way that I extract the data from the csv file AND convert it to a format that can handle scientific notation so that I can plot and analyze it?

Sorry for the long post but I don't know where to turn to.

1 Answer 1

1

When you want to work on csv file, the pandas library is really usefull.

Here, it makes everything much simpler:

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

To plot the Force as a function of the time, you just have to do the following:

plt.plot(df['Time'],df['Force'])
Sign up to request clarification or add additional context in comments.

1 Comment

This is a perfect implementation of dataframes. Nice work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.