135 KiB
135 KiB
Reading files
import pandas as pd
# pandas is a Python package providing fast, flexible, and expressive data
# structures designed to make working with “relational” or “labeled” data
# both easy and intuitive.
CSV files
!cat data.csv
x;y;er
1;1;0.2
2;4;0.2
3;8;0.3
4;17;0.4
5;24;0.2
# read file
= pd.read_csv("data.csv",delimiter=";")
d print(d)
x y er
0 1 1 0.2
1 2 4 0.2
2 3 8 0.3
3 4 17 0.4
4 5 24 0.2
# taking values from headers
=d["x"]
x=d["y"]
y=d["er"]
eprint(x,y)
0 1
1 2
2 3
3 4
4 5
Name: x, dtype: int64 0 1
1 4
2 8
3 17
4 24
Name: y, dtype: int64
# taking values from colon .iloc[row,col] or .loc[row,col]
=d.iloc[:,0]
x=d.iloc[:,1]
y=d.iloc[:,2]
eprint(x,y)
0 1
1 2
2 3
3 4
4 5
Name: x, dtype: int64 0 1
1 4
2 8
3 17
4 24
Name: y, dtype: int64
Raw files with headers
# text files
!cat data.txt
x y er
1 1 0.2
2 4 0.2
3 8 0.3
# read file
= pd.read_csv("data.txt")
dtext print(dtext)
x y er
0 1 1 0.2
1 2 4 0.2
2 3 8 0.3
Raw files without headers
# raw values
!cat data.raw
1 1 0.2
2 4 0.2
3 8 0.3
# read raw file
= pd.read_csv("data.raw",delimiter=" ", header = None)
draw print(draw)
0 1 2
0 1 1 0.2
1 2 4 0.2
2 3 8 0.3
2] draw.iloc[:,
0 0.2
1 0.2
2 0.3
Name: 2, dtype: float64
Formatted file
# read raw file
= [11,10]
fwidths = pd.read_fwf("expe.dat",delimiter=" ",header = None, widths = fwidths)
dexpe print(dexpe)
0 1
0 0.0 0.894520
1 100.0 0.849616
2 200.0 0.806313
3 300.0 0.763861
4 400.0 0.722299
5 500.0 0.681680
6 600.0 0.642211
7 700.0 0.603411
8 800.0 0.565199
9 900.0 0.528873
10 1000.0 0.492817
More formats
see references
# import image module
from IPython.display import Image
# get the image
="02_io_readwrite.svg", width=500, height=500) Image(url
Plotting data
import matplotlib.pyplot as plt
# read raw file
= [11,10]
fwidths = pd.read_fwf("expe.dat",delimiter=" ",header = None, widths = fwidths)
dexpe print(dexpe)
0 1
0 0.0 0.894520
1 100.0 0.849616
2 200.0 0.806313
3 300.0 0.763861
4 400.0 0.722299
5 500.0 0.681680
6 600.0 0.642211
7 700.0 0.603411
8 800.0 0.565199
9 900.0 0.528873
10 1000.0 0.492817
# get x and y
=dexpe.iloc[:,0]
x=dexpe.iloc[:,1] y
# plot
= plt.figure(figsize=(6,6)) # s
fig = plt.subplot()
ax "o",label="Experimental data")
ax.plot(x,y,0,2])
ax.set_ylim(["t[s]")
ax.set_xlabel("V[t]")
ax.set_ylabel("Temporal Evolution of Volume")
ax.set_title( ax.legend()
<matplotlib.legend.Legend at 0x7fbffabe9df0>
Fitting : linear
from scipy.optimize import curve_fit
import numpy as np
def func(x, a, b):
return a * x + b
= curve_fit(func, x, y)
poptprint(popt)
(array([-4.01437909e-04, 8.87155318e-01]), array([[ 1.96723961e-11, -9.83619802e-09],
[-9.83619802e-09, 6.88533863e-06]]))
=popt[0] [a,b]
= plt.figure(figsize=(6,6)) #
fig = plt.subplot()
ax "o",label="Experimental data")
ax.plot(x,y,*popt[0]),label="Linear model")
ax.plot(x,func(x,0,2])
ax.set_ylim(["t[s]")
ax.set_xlabel("V[t]")
ax.set_ylabel("Temporal Evolution of Volume")
ax.set_title( ax.legend()
<matplotlib.legend.Legend at 0x7fbffb10bf40>
Fitting : non linear
# read raw file
= [6,6,6]
fwidths = pd.read_fwf("power.dat",delimiter=" ",header = None, widths = fwidths)
dexpe print(dexpe)
0 1 2
0 0.102 0.319 0.565
1 0.204 0.452 0.672
2 0.306 0.553 0.744
3 0.408 0.639 0.799
4 0.510 0.714 0.845
5 0.612 0.782 0.885
6 0.714 0.845 0.919
7 0.816 0.904 0.951
8 0.918 0.958 0.979
9 1.020 1.010 1.005
10 1.122 1.059 1.029
11 1.224 1.107 1.052
12 1.327 1.152 1.073
13 1.429 1.195 1.093
14 1.531 1.237 1.112
15 1.633 1.278 1.130
16 1.735 1.317 1.148
17 1.837 1.355 1.164
18 1.939 1.392 1.180
19 2.041 1.429 1.195
20 2.143 1.464 1.210
21 2.245 1.498 1.224
22 2.347 1.532 1.238
23 2.449 1.565 1.251
24 2.551 1.597 1.264
25 2.653 1.629 1.276
26 2.755 1.660 1.288
27 2.857 1.690 1.300
28 2.959 1.720 1.312
29 3.061 1.750 1.323
30 3.163 1.779 1.334
31 3.265 1.807 1.344
32 3.367 1.835 1.355
33 3.469 1.863 1.365
34 3.571 1.890 1.375
35 3.673 1.917 1.384
36 3.776 1.943 1.394
37 3.878 1.969 1.403
38 3.980 1.995 1.412
39 4.082 2.020 1.421
40 4.184 2.045 1.430
41 4.286 2.070 1.439
42 4.388 2.095 1.447
43 4.490 2.119 1.456
44 4.592 2.143 1.464
45 4.694 2.167 1.472
46 4.796 2.190 1.480
47 4.898 2.213 1.488
48 5.000 2.236 1.495
# get x and y
=dexpe.iloc[:,0]
x =dexpe.iloc[:,1]
y1=dexpe.iloc[:,2] y2
# plot
= plt.figure(figsize=(6,6)) # s
fig = plt.subplot()
ax "o",label="Experimental data 1")
ax.plot(x,y1,"o",label="Experimental data 2")
ax.plot(x,y2,0,2])
ax.set_ylim(["t[s]")
ax.set_xlabel("V[t]")
ax.set_ylabel("Temporal Evolution of Volume")
ax.set_title( ax.legend()
<matplotlib.legend.Legend at 0x7fbfe90ebfd0>
log-log
# plot
= plt.figure(figsize=(6,6)) # s
fig = plt.subplot()
ax "o",label="Experimental data 1")
ax.plot(x,y1,"o",label="Experimental data 2")
ax.plot(x,y2,"t[s]")
ax.set_xlabel("V[t]")
ax.set_ylabel("Temporal Evolution of Volume")
ax.set_title(
ax.legend() ax.loglog()
[]
def func(x, a, b):
return a * x + b
= curve_fit(func, np.log(x), np.log(y1)) popt
0]
popt[=popt[0][0]
a=popt[0][1]
bprint("%.3f" % a)
print("%.3f" % b)
0.500
-0.000
# plot
= plt.figure(figsize=(6,6)) # s
fig = plt.subplot()
ax "o",label="Experimental data 1")
ax.plot(x,y1,**a+b,"-",label="Model 1")
ax.plot(x,x"t[s]")
ax.set_xlabel("V[t]")
ax.set_ylabel("Temporal Evolution of Volume")
ax.set_title(
ax.legend() ax.loglog()
[]
Errors bar
# read file
= pd.read_csv("data.csv",delimiter=";")
d # taking values from headers
=d["x"]
x=d["y"]
y=d["er"]
eprint(x,y,e)
0 1
1 2
2 3
3 4
4 5
Name: x, dtype: int64 0 1
1 4
2 8
3 17
4 24
Name: y, dtype: int64 0 0.2
1 0.2
2 0.3
3 0.4
4 0.2
Name: er, dtype: float64
= plt.figure(figsize=(6,6)) # s
fig = plt.subplot()
ax 0.1*y,e*2, marker='.', linestyle="none", label="data")
ax.errorbar(x, ax.legend()
<matplotlib.legend.Legend at 0x7fbfc8070bb0>
# data generation
=np.linspace(0,5)
x= np.power(x,0.5)
y1 = np.power(x,0.25)
y2 #print(np.size(x))
#for i in range(50):
# print("%.3f" % x[i],"%.3f" % np.power(x[i],0.5),"%.3f" % np.power(x[i],0.25))
#print(x,np.power(x,1./2))
= pd.Series(x)
sx = pd.Series(y1)
sy1 = pd.Series(y2) sy2
= pd.concat([sx,sy1,sy2],axis=1) nd
"test.csv") nd.to_csv(
= pd.read_csv("test.csv",delimiter=",") d
d
Unnamed: 0 | 0 | 1 | 2 | |
---|---|---|---|---|
0 | 0 | 0.000000 | 0.000000 | 0.000000 |
1 | 1 | 0.102041 | 0.319438 | 0.565189 |
2 | 2 | 0.204082 | 0.451754 | 0.672126 |
3 | 3 | 0.306122 | 0.553283 | 0.743830 |
4 | 4 | 0.408163 | 0.638877 | 0.799298 |
5 | 5 | 0.510204 | 0.714286 | 0.845154 |
6 | 6 | 0.612245 | 0.782461 | 0.884568 |
7 | 7 | 0.714286 | 0.845154 | 0.919323 |
8 | 8 | 0.816327 | 0.903508 | 0.950530 |
9 | 9 | 0.918367 | 0.958315 | 0.978936 |
10 | 10 | 1.020408 | 1.010153 | 1.005063 |
11 | 11 | 1.122449 | 1.059457 | 1.029299 |
12 | 12 | 1.224490 | 1.106567 | 1.051935 |
13 | 13 | 1.326531 | 1.151751 | 1.073197 |
14 | 14 | 1.428571 | 1.195229 | 1.093265 |
15 | 15 | 1.530612 | 1.237179 | 1.112286 |
16 | 16 | 1.632653 | 1.277753 | 1.130377 |
17 | 17 | 1.734694 | 1.317078 | 1.147640 |
18 | 18 | 1.836735 | 1.355262 | 1.164157 |
19 | 19 | 1.938776 | 1.392399 | 1.180000 |
20 | 20 | 2.040816 | 1.428571 | 1.195229 |
21 | 21 | 2.142857 | 1.463850 | 1.209897 |
22 | 22 | 2.244898 | 1.498298 | 1.224050 |
23 | 23 | 2.346939 | 1.531972 | 1.237729 |
24 | 24 | 2.448980 | 1.564922 | 1.250968 |
25 | 25 | 2.551020 | 1.597191 | 1.263800 |
26 | 26 | 2.653061 | 1.628822 | 1.276253 |
27 | 27 | 2.755102 | 1.659850 | 1.288352 |
28 | 28 | 2.857143 | 1.690309 | 1.300119 |
29 | 29 | 2.959184 | 1.720228 | 1.311575 |
30 | 30 | 3.061224 | 1.749636 | 1.322738 |
31 | 31 | 3.163265 | 1.778557 | 1.333626 |
32 | 32 | 3.265306 | 1.807016 | 1.344253 |
33 | 33 | 3.367347 | 1.835033 | 1.354634 |
34 | 34 | 3.469388 | 1.862629 | 1.364782 |
35 | 35 | 3.571429 | 1.889822 | 1.374708 |
36 | 36 | 3.673469 | 1.916630 | 1.384424 |
37 | 37 | 3.775510 | 1.943067 | 1.393939 |
38 | 38 | 3.877551 | 1.969150 | 1.403264 |
39 | 39 | 3.979592 | 1.994891 | 1.412406 |
40 | 40 | 4.081633 | 2.020305 | 1.421374 |
41 | 41 | 4.183673 | 2.045403 | 1.430176 |
42 | 42 | 4.285714 | 2.070197 | 1.438818 |
43 | 43 | 4.387755 | 2.094697 | 1.447307 |
44 | 44 | 4.489796 | 2.118914 | 1.455649 |
45 | 45 | 4.591837 | 2.142857 | 1.463850 |
46 | 46 | 4.693878 | 2.166536 | 1.471916 |
47 | 47 | 4.795918 | 2.189959 | 1.479851 |
48 | 48 | 4.897959 | 2.213133 | 1.487660 |
49 | 49 | 5.000000 | 2.236068 | 1.495349 |
Unnamed: 0 | 0 | 1 | 2 | |
---|---|---|---|---|
10 | 10 | 1.020408 | 1.010153 | 1.005063 |
11 | 11 | 1.122449 | 1.059457 | 1.029299 |
12 | 12 | 1.224490 | 1.106567 | 1.051935 |
13 | 13 | 1.326531 | 1.151751 | 1.073197 |
14 | 14 | 1.428571 | 1.195229 | 1.093265 |
15 | 15 | 1.530612 | 1.237179 | 1.112286 |
16 | 16 | 1.632653 | 1.277753 | 1.130377 |
17 | 17 | 1.734694 | 1.317078 | 1.147640 |
18 | 18 | 1.836735 | 1.355262 | 1.164157 |
19 | 19 | 1.938776 | 1.392399 | 1.180000 |
20 | 20 | 2.040816 | 1.428571 | 1.195229 |
21 | 21 | 2.142857 | 1.463850 | 1.209897 |
22 | 22 | 2.244898 | 1.498298 | 1.224050 |
23 | 23 | 2.346939 | 1.531972 | 1.237729 |
24 | 24 | 2.448980 | 1.564922 | 1.250968 |
25 | 25 | 2.551020 | 1.597191 | 1.263800 |
26 | 26 | 2.653061 | 1.628822 | 1.276253 |
27 | 27 | 2.755102 | 1.659850 | 1.288352 |
28 | 28 | 2.857143 | 1.690309 | 1.300119 |
29 | 29 | 2.959184 | 1.720228 | 1.311575 |
30 | 30 | 3.061224 | 1.749636 | 1.322738 |
31 | 31 | 3.163265 | 1.778557 | 1.333626 |
32 | 32 | 3.265306 | 1.807016 | 1.344253 |
33 | 33 | 3.367347 | 1.835033 | 1.354634 |
34 | 34 | 3.469388 | 1.862629 | 1.364782 |
35 | 35 | 3.571429 | 1.889822 | 1.374708 |
36 | 36 | 3.673469 | 1.916630 | 1.384424 |
37 | 37 | 3.775510 | 1.943067 | 1.393939 |
38 | 38 | 3.877551 | 1.969150 | 1.403264 |
39 | 39 | 3.979592 | 1.994891 | 1.412406 |
40 | 40 | 4.081633 | 2.020305 | 1.421374 |
41 | 41 | 4.183673 | 2.045403 | 1.430176 |
42 | 42 | 4.285714 | 2.070197 | 1.438818 |
43 | 43 | 4.387755 | 2.094697 | 1.447307 |
44 | 44 | 4.489796 | 2.118914 | 1.455649 |
45 | 45 | 4.591837 | 2.142857 | 1.463850 |
46 | 46 | 4.693878 | 2.166536 | 1.471916 |
47 | 47 | 4.795918 | 2.189959 | 1.479851 |
48 | 48 | 4.897959 | 2.213133 | 1.487660 |
49 | 49 | 5.000000 | 2.236068 | 1.495349 |