[Tutor] Appending an extra column in a data file

Sayan Chatterjee sayanchatterjee at gmail.com
Wed Apr 10 20:48:05 CEST 2013


Hi Andre,

I figured it out. Need not reply of np.savetxt for storing array simply
f.wite() is doing fine.

while i < 999:
    print i
    fo.write('{0:f}  {1:f}  {2:f}\n'.format(pp_za[i], pv_za[i],t))
    i = i + 1

Eager to know that single file thing though!

Sayan


On 10 April 2013 23:52, Sayan Chatterjee <sayanchatterjee at gmail.com> wrote:

> Hi Andre,
>
> Is it that? We can have different plots and hence animation from a single
> data file.Very good!...if you have some time in your disposal, it will be
> kind of you to illumine in this matter...may be some useful link will do.
>
> I have written this small code from scratch,there's no 'get it to work'
> business...:)
>
> Here is the total code...not attaching as you might have problem opening
> random files from internet....:)
>
> #!usr/bin/python
>
> import numpy as np
> import matplotlib.pyplot as plt
> import itertools as i
> import pylab
>
> K = 3.14
> t = 0
>
> # Allotment of particles
>
> pp_init = np.linspace(0,1.99799799,999)
>
> print pp_init
>
> # Velocity array
>
> while t < 1:
>
>
>   pp_za = pp_init + t*K*np.sin(K*pp_init)
>
> # Periodic Boundary Condition
>
>   for i in range(0,999):
>
>     if pp_za[i] < 0:
>       pp_za[i] = 2 - abs(pp_za[i])
>     if pp_za[i] > 2:
>       pp_za[i] = pp_za[i] % 2
>
>   pv_za = +K*np.sin(K*pp_init)
>
>   fname = 'file_' + str(t) + '.dat'
>
> # Generating dataset for Phase Space diagram
>   np.savetxt(fname, np.array([pp_za,pv_za,t]), '%f')
>
>   t = t + 0.01
>
> # Plot using Matplotlib
>
> i = 0
>
> while i < 1:
>   fname = 'file_' + str(i) + '.dat'
>   f=open(fname,"r")
>   x,y = np.loadtxt(fname, unpack=True)
>   #plt.plot(x,y,linewidth=2.0)
>   plt.scatter(x,y)
>   plt.ylim(-8,8)
>   plt.xlim(0,2)
>   plt.ylabel('Velocity')
>   plt.xlabel('X')
>   pylab.savefig(fname + '.png')
>   plt.cla()
>   f.close()
>   i = i  + 0.001
>
> Regards,
> Sayan
>
>
>
>
> On 10 April 2013 23:36, Andre' Walker-Loud <walksloud at gmail.com> wrote:
>
>> Hi Sayan,
>>
>> > Yup it's exactly what I want!
>> >
>> > I want many data files,not one...to make an animation out of it. For a
>> data file t is constant.
>>
>> You should not need many data files to make an animation.  If you write
>> your loops correctly, you can take a single data file with all the data.
>>  If you are using code you did not write, and just want to get it to work,
>> I understand that, but would encourage you also to figure out how to do it
>> all with single data file.  Less files makes a files system and user
>> happier.
>>
>> > the solution you have just mentioned i.e np.array([t,pp_za,pv_za]) is
>> giving the following error:
>> >
>> >
>> > Traceback (most recent call last):
>> >   File "ZA_Phase_Plot.py", line 38, in <module>
>> >     np.savetxt(fname, np.array([pp_za,pv_za,t]).T, '%f')
>> >   File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 979,
>> in savetxt
>> >     fh.write(asbytes(format % tuple(row) + newline))
>> > TypeError: float argument required, not numpy.ndarray
>> >
>> > What to do? :-|
>>
>> There are pieces of the code snipet you sent which are not defined, so it
>> is not clear why you are getting this error.  How long is the file
>> "ZA_Phase_Plot.py"?  At least you can send from line 0 to where you did
>> before, so all the variables are defined.  That should help.
>>
>> Andre
>>
>>
>>
>>
>
>
> --
>
>
> --------------------------------------------------------------------------
> *Sayan  Chatterjee*
> Dept. of Physics and Meteorology
> IIT Kharagpur
> Lal Bahadur Shastry Hall of Residence
> Room AB 205
> Mob: +91 9874513565
> blog: www.blissprofound.blogspot.com
>
> Volunteer , Padakshep
> www.padakshep.org
>



-- 


--------------------------------------------------------------------------
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

Volunteer , Padakshep
www.padakshep.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130411/aa27143a/attachment-0001.html>


More information about the Tutor mailing list