Difference between revisions of "Matplotlib disp spin.out"

From relax wiki
Jump to navigation Jump to search
(Forced creation of a TOC - this will improve the formatting on the main page 'Did you know...' section.)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
__TOC__
 +
 
== About ==
 
== About ==
  
Line 5: Line 7:
 
<source lang="text">
 
<source lang="text">
 
# Experiment_name    Field_strength_(MHz) Tilt_angle_(rad)    R2eff_(measured)    R2eff_(back_calc)    R2eff_errors         
 
# Experiment_name    Field_strength_(MHz) Tilt_angle_(rad)    R2eff_(measured)    R2eff_(back_calc)    R2eff_errors         
'R1rho'                    799.777399100            -1.530741    9.699126098118711    9.585976725687559    0.027663458874525
+
'R1rho'                    799.777399100            1.530741    9.699126098118711    9.585976725687559    0.027663458874525
'R1rho'                    799.777399100            -1.544277    9.678896177797514    9.526265376687768    0.064301737965685
+
'R1rho'                    799.777399100            1.544277    9.678896177797514    9.526265376687768    0.064301737965685
 
...
 
...
 
</source>
 
</source>
 +
 
== Code ==
 
== Code ==
 
<source lang="python">
 
<source lang="python">
 +
import os
 
import sys
 
import sys
 
import numpy as np
 
import numpy as np
Line 21: Line 25:
  
 
infile = sys.argv[1]
 
infile = sys.argv[1]
 +
filename , fileext = os.path.splitext(infile)
 
print("Input file is: %s"%infile)
 
print("Input file is: %s"%infile)
  
Line 33: Line 38:
 
fileindex = [w.replace(')', '') for w in fileindex]
 
fileindex = [w.replace(')', '') for w in fileindex]
 
fileindex = [w.replace('.', '') for w in fileindex]
 
fileindex = [w.replace('.', '') for w in fileindex]
 +
fileindex = [w.replace('-', '') for w in fileindex]
  
 
# Prepare to define what file exist of
 
# Prepare to define what file exist of
Line 53: Line 59:
 
plt.figure()
 
plt.figure()
 
plt.errorbar(xval, R2eff_meas, yerr=R2eff_meas_err, label=fileindex[3], fmt='o')
 
plt.errorbar(xval, R2eff_meas, yerr=R2eff_meas_err, label=fileindex[3], fmt='o')
 +
plt.xlabel(fileindex[2])
 +
plt.ylabel(fileindex[3])
 +
if not calc_not_avail:
 +
    plt.plot(xval, R2eff_calc, 'o', label=fileindex[4])
 +
plt.legend(loc='best')
 +
plt.grid(True)
 +
plt.title("%s as function of %s"%(fileindex[3], fileindex[2]))
 +
plt.savefig("matplotlib_%s.png"%filename)
 +
 +
 +
plt.show()
 
</source>
 
</source>
  
To run
+
== To run ==
 
<source lang="bash">
 
<source lang="bash">
python -i testmatplotlib.py disp_13_N.out
+
python testmatplotlib.py disp_52_N.out
python -i testmatplotlib.py disp_theta_13_N.out
+
python testmatplotlib.py disp_theta_52_N.out  
 +
python -i testmatplotlib.py disp_w_eff_52_N.out
 
</source>
 
</source>
  
 
== See also ==
 
== See also ==
 
[[Category:Matplotlib]]
 
[[Category:Matplotlib]]

Latest revision as of 13:45, 15 October 2015

About

Input file

Could look like this

# Experiment_name    Field_strength_(MHz) Tilt_angle_(rad)     R2eff_(measured)     R2eff_(back_calc)    R2eff_errors        
'R1rho'                     799.777399100            1.530741    9.699126098118711    9.585976725687559    0.027663458874525
'R1rho'                     799.777399100            1.544277    9.678896177797514    9.526265376687768    0.064301737965685
...

Code

import os
import sys
import numpy as np
import matplotlib.pyplot as plt

# Exit if no *.out file
if len(sys.argv) != 2:
    print("You have to provide a input .out file")
    sys.exit()

infile = sys.argv[1]
filename , fileext = os.path.splitext(infile)
print("Input file is: %s"%infile)

# Get index of file
with open(infile, 'r') as f:
    for line in f:
        if line.startswith('# Experiment_name'):
            fileindex = line.split()[1:]

# Remove not allowed letters
fileindex = [w.replace('(', '') for w in fileindex]
fileindex = [w.replace(')', '') for w in fileindex]
fileindex = [w.replace('.', '') for w in fileindex]
fileindex = [w.replace('-', '') for w in fileindex]

# Prepare to define what file exist of
fdtype="S20"
for item in fileindex[1:]:
    fdtype += ",f8"

print("Fileindex is: %s"%fileindex)
print("Value types are: %s"%fdtype)

data = np.genfromtxt(fname=infile, dtype=fdtype, names=fileindex)

xval = data[fileindex[2]]
R2eff_meas = data[fileindex[3]]
R2eff_meas_err = data[fileindex[5]]
R2eff_calc = data[fileindex[4]]

calc_not_avail = np.isnan(R2eff_calc[0])

plt.figure()
plt.errorbar(xval, R2eff_meas, yerr=R2eff_meas_err, label=fileindex[3], fmt='o')
plt.xlabel(fileindex[2])
plt.ylabel(fileindex[3])
if not calc_not_avail:
    plt.plot(xval, R2eff_calc, 'o', label=fileindex[4])
plt.legend(loc='best')
plt.grid(True)
plt.title("%s as function of %s"%(fileindex[3], fileindex[2]))
plt.savefig("matplotlib_%s.png"%filename)


plt.show()

To run

python testmatplotlib.py disp_52_N.out
python testmatplotlib.py disp_theta_52_N.out 
python -i testmatplotlib.py disp_w_eff_52_N.out

See also