[Tutor] Appending an extra column in a data file

Andre' Walker-Loud walksloud at gmail.com
Wed Apr 10 19:47:08 CEST 2013


Hi Sayan,

> Well,this is the concerned snippet of the code:
> 
> 
> 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

To answer your question, to add "t" to the array, you can simply replace the current np.array command with either of the following

np.array([pp_za,pv_za,t])

or 

np.array([t,pp_za,pv_za])

depending if you want "t" is the first element of the data or the last.
Also, you are "transposing" the array with the ".T", so this will write the data in sequential lines, rather than a single line (row) with multiple entries.  I just bring it up because it is not the "obvious" way to pack the data.

Also, it looks like you are generating a separate file for each "t".
Is this what you want?
I assume you want a single data file that stores this info, not many data files.


Andre





> 
> And the sample data generated is : 
> 
> 0.105728 0.098678
> 0.126865 0.118406
> 0.147998 0.138128
> 0.169126 0.157845
> 0.190247 0.177556
> 0.211362 0.197259
> 0.232469 0.216955
> 0.253567 0.236643
> 0.274657 0.256321
> 0.295737 0.275989
> 0.316806 0.295646
> 
> Precisely what I want is, I want to add the 't' (over which the while loop is run) in the 3 rd column. 't' is float and constant for each looping. If you know what a phase plot is(doesn't matter if you don't), I want to see the phase plot evolving with time. Thus the need of a time axis and hence plot with V(velocity-Yaxis), X ( Position -X axis) and t (Time-Z axis).
> 
> I hope your first example might help.Though,I think,I need some tweaking to fit to my needs.
> 
> Regards,
> Sayan
> 
> 
> On 10 April 2013 22:38, Andre' Walker-Loud <walksloud at gmail.com> wrote:
> Hi Sayan,
> 
> > Thank Andre for your prompt answer.
> 
> No problem.
> 
> > I'll figure out the plotting issue once the dat files are made. So it's the primary concern.
> > For an example I am attaching a dat file herewith. The two columns here are 2 numpy arrays.I want to add a third column, to be precise, I want to print a parameter value on the third column of the file.
> 
> Let me try again.
> The reason the matplotlib list would be a better place is this is a general python list, and most people are not familiar with numpy.  However, most people who use matplotlib are familiar with numpy.
> 
> I am hoping you can describe precisely the structure of the data.  Maybe show a little code on how it is created, or how you access it.  I am not keen to open "random" files from the internet.  As two examples of how I think your code might be packed
> 
> 1/
> '''
> x = numpy.zeros([10]) # 1D numpy array of dimension 10
> y = numpy.zeros([10]) # 1D numpy array of dimension 10
> your_data = []
> your_data.append(x)
> your_data.append(y)
> '''
> 
> so now your data is a table with two entries, and each entry is a numpy array.
> You have in mind adding a third entry to the table with just floats.
> 
> 2/
> '''
> your_data = numpy.zeros([10,10]) # initialize a 2D numpy array with all zeros
> for i in range(your_data.shape[0]):
>     for j in range(your_data.shape[1]):
>         your_data[i,j] = data[i][j] # I am assuming the data is imported already and called data and is in a python list/table format
> '''
> 
> Now you want to make a new column or row for your data file, which contains floats.  Well, all the entries inside the numpy array are already floats, so it is not clear to me why you want a new column that is not a numpy array. So it would be nice if you could precisely describe what you currently have and what you want.
> 
> 
> Hope this helps,
> 
> 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



More information about the Tutor mailing list