Overview
Example
# Example Python program that creates the points of a sine wave and
# stores them on a file using numpy savetxt() function
import numpy as np
import matplotlib.pyplot as plot
path = "sinewave.txt"
# Generate X values of the sine wave
time = np.arange(0, 10, 0.1);
# Generate amplitude of the sine wave
amplitude = np.sin(time)
# Make a two-dimensional ndarray - time vs sine(time)
sineWaveArray = np.vstack((time, amplitude)).T
print(sineWaveArray)
print(sineWaveArray.shape)
# Write ndarray into the file in floating point format.
# The default is scientific notation.
np.savetxt(path, sineWaveArray, fmt='%f')
print("2d array saved to:%s"%path)Output
[[ 0. 0. ]
[ 0.1 0.09983342]
[ 0.2 0.19866933]
[ 0.3 0.29552021]
[ 0.4 0.38941834]
[ 0.5 0.47942554]
[ 0.6 0.56464247]
[ 0.7 0.64421769]
[ 0.8 0.71735609]
[ 0.9 0.78332691]
[ 1. 0.84147098]
[ 1.1 0.89120736]
[ 1.2 0.93203909]
[ 1.3 0.96355819]
[ 1.4 0.98544973]
[ 1.5 0.99749499]
[ 1.6 0.9995736 ]
[ 1.7 0.99166481]
[ 1.8 0.97384763]
[ 1.9 0.94630009]
[ 2. 0.90929743]
[ 2.1 0.86320937]
[ 2.2 0.8084964 ]
[ 2.3 0.74570521]
[ 2.4 0.67546318]
[ 2.5 0.59847214]
[ 2.6 0.51550137]
[ 2.7 0.42737988]
[ 2.8 0.33498815]
[ 2.9 0.23924933]
[ 3. 0.14112001]
[ 3.1 0.04158066]
[ 3.2 -0.05837414]
[ 3.3 -0.15774569]
[ 3.4 -0.2555411 ]
[ 3.5 -0.35078323]
[ 3.6 -0.44252044]
[ 3.7 -0.52983614]
[ 3.8 -0.61185789]
[ 3.9 -0.68776616]
[ 4. -0.7568025 ]
[ 4.1 -0.81827711]
[ 4.2 -0.87157577]
[ 4.3 -0.91616594]
[ 4.4 -0.95160207]
[ 4.5 -0.97753012]
[ 4.6 -0.993691 ]
[ 4.7 -0.99992326]
[ 4.8 -0.99616461]
[ 4.9 -0.98245261]
[ 5. -0.95892427]
[ 5.1 -0.92581468]
[ 5.2 -0.88345466]
[ 5.3 -0.83226744]
[ 5.4 -0.77276449]
[ 5.5 -0.70554033]
[ 5.6 -0.63126664]
[ 5.7 -0.55068554]
[ 5.8 -0.46460218]
[ 5.9 -0.37387666]
[ 6. -0.2794155 ]
[ 6.1 -0.1821625 ]
[ 6.2 -0.0830894 ]
[ 6.3 0.0168139 ]
[ 6.4 0.1165492 ]
[ 6.5 0.21511999]
[ 6.6 0.31154136]
[ 6.7 0.40484992]
[ 6.8 0.49411335]
[ 6.9 0.57843976]
[ 7. 0.6569866 ]
[ 7.1 0.72896904]
[ 7.2 0.79366786]
[ 7.3 0.85043662]
[ 7.4 0.8987081 ]
[ 7.5 0.93799998]
[ 7.6 0.96791967]
[ 7.7 0.98816823]
[ 7.8 0.99854335]
[ 7.9 0.99894134]
[ 8. 0.98935825]
[ 8.1 0.96988981]
[ 8.2 0.94073056]
[ 8.3 0.90217183]
[ 8.4 0.85459891]
[ 8.5 0.79848711]
[ 8.6 0.7343971 ]
[ 8.7 0.66296923]
[ 8.8 0.58491719]
[ 8.9 0.50102086]
[ 9. 0.41211849]
[ 9.1 0.31909836]
[ 9.2 0.22288991]
[ 9.3 0.12445442]
[ 9.4 0.02477543]
[ 9.5 -0.07515112]
[ 9.6 -0.17432678]
[ 9.7 -0.27176063]
[ 9.8 -0.36647913]
[ 9.9 -0.45753589]]
(100, 2)
2d array saved to:sinewave.txt