I am trying to plot the fall of an object (an optical fork to be precise) as a function of time in order to verify that the law of gravity is indeed 9.81. The different data are supposed to represent the passage at each slot. The different slits are spaced 1 centimeter apart and there are 11 slits in all. I measured these data with an Arduino setup and I plot the graph and fit with Python. I have the data in a CSV file but when I run my code, I get an
KeyError: 'T (s)'
I don't understand because the column T (s) is present in my DataFrame.
Here is my CSV File with name 'Test.csv' (I specify that I don't want to select the first and last value of column T (i.e. 3.514 and 3.636) and that I don't want to read the Distance column):
| T (s) | Distance (m) |
| ----- | ------------ |
| 3.514 | 0.000 |
| 3.524 | 0.010 |
| 3.536 | 0.020 |
| 3.548 | 0.030 |
| 3.562 | 0.040 |
| 3.574 | 0.050 |
| 3.582 | 0.060 |
| 3.592 | 0.070 |
| 3.6 | 0.080 |
| 3.61 | 0.090 |
| 3.618 | 0.100 |
| 3.626 | 0.110 |
| 3.636 | 0.120 |
And here is my code:
import numpy as np # For the calculation
import pandas as pd # To read files
import matplotlib.pyplot as plt # To draw curves
import scipy.optimize as opt # For the adjustment
# Raw data
data = pd.read_csv("Test.csv") # Opening the data file
z = -0.01 * np.linspace(1, 11, 11)
x = data['T (s)']
x_util = np.array(x[3.524:3.626]) # extracts data between 3.524 and 3.626 s
# Definition of the free fall function
g = 9.81 # the acceleration of gravity
def f(x_util,t0,h0): # Definition of the fitting function
return -0.5*g*(x_util-t0)**2 h0
# Data adjustment
init_param = [0 , 0] # Initial values t0=0, h0=0
final_param , var = opt.curve_fit(f,x_util,z,init_param)
# Optimal function
tt = np.linspace(final_param[0], 100e-3,100)
hh = f(tt, *final_param) # Reconstruction of the fitted curve
# Plot of analyzed data
plt.clf() # Plot of data and fit
plt.xlabel("Time (s)")
plt.ylabel("Height (m)")
legend = "t0 = %f ms, h0 = %f centimeter " % (final_param[0]*1000,final_param[1]*100)
plt.plot(tt,hh,"r--",label=legend) # The adjustment
plt.plot(x_util,z,"bo", label="Data") # The data
plt.legend()
Do you know where this error can come from?
CodePudding user response:
Actually looking at the df.info it seems that the separator,at least judging by the column name, is a semicolon. Also the column name should be "Time (s)". Please try:
data=pd.read_csv("Test.csv",sep=";")
z = -0.01 * np.linspace(1, 11, 11)
x = data['Time (s)']