QuantumATK Forum

QuantumATK => General Questions and Answers => Topic started by: ams_nanolab on August 6, 2015, 14:05

Title: Saving data from script
Post by: ams_nanolab on August 6, 2015, 14:05
I have a python script to see a plot in ATK, but I cant save it into a data file, here's the plot command
--------------------------------------
# Plot the data
import pylab
pylab.plot(energ,alpha,'b', label='plot')
pylab.xlabel(r"Energy (eV)", size=16)
pylab.ylabel(r"alpha",size=16)
pylab.show()
-------------------------

I want to save the data for plotting into a .dat file , consisting of two columns .. how do I do that?
Title: Re: Saving data from script
Post by: Anders Blom on August 7, 2015, 01:29
This is pretty basic Python...

Code: python
with open("alpha.dat", "w") as f:
    for e,a in zip(energy,alpha):
        f.write("%g\t%g\n" % (e,a))

Of course it can be done less elegantly, and perhaps easier to understand:

Code: python
f = open("alpha.dat", "w")
for i in range(len(energy)):
    s = str(energy[i]) + "\t" + str(alpha[i])
    f.write(s+"\n")
f.close()
Title: Re: Saving data from script
Post by: ams_nanolab on August 7, 2015, 13:51
Thanks.