I have a text file which contains 2 columns separated by a tab, containing some data that I would like to read into arrays and perform some simple operations for instance plot the data. The data in the second column is in scientific notation and can takes extremely small values such varying from order of magnitude 10e-27 10e-50. For instance here is a sample of the data
0.00521135 -1.197189e-31
0.00529274 -7.0272737e-32
0.00530917 -6.0163467e-32
0.00532565 -4.9990405e-32
0.00534218 -3.9747722e-32
0.00535876 -2.9457271e-32
0.0053754 -1.9094542e-32
0.00539208 -8.6847519e-33
0.00540882 1.7851373e-33
0.00542561 1.2288483e-32
0.00544245 2.2850705e-32
0.00545934 3.3432858e-32
0.00547629 4.4084594e-32
0.00549329 5.4765499e-32
0.00551034 6.5491709e-32
Here is what my code looks like :
import numpy as np
import matplotlib.pyplot as plt
with open('data.dat', 'r') as f2:
lines = f2.readlines()
data = [line.split()for line in lines]
data2 = np.asfarray(data)
x1 = data2[:,0]
y1 = data2[:,1]
plt.plot(x1, y1)
plt.show()
I have used this code to test on sample data (in .dat format) files and it seems to work fine, however when I run this code on my data set it gives me the following error.
Traceback (most recent call last):
File "read_txt_col.py", line 17, in <module>
data2 = np.asfarray(data)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages /numpy/lib/type_check.py", line 103, in asfarray
return asarray(a,dtype=dtype)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numeric.py", line 235, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
Could someone please help!!
loadtxt() function. The loadtxt() function is used to load data from a text file. Each row in the text file must have the same number of values.
You need x = np. zeros(N) , etc.: this declares the arrays as float arrays. This is the standard way of putting zeros in an array ( np. tile() is convenient for creating a tiling with a fixed array).
The function returns an n-dimensional NumPy array of values found in the text.
Don't reinvent the wheel!, it would be much more easy to use numpy.loadtxt
:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> data = np.loadtxt('data.dat')
>>> x1 = data[:,0]
>>> y1 = data[:,1]
>>> plt.plot(x1, y1)
>>> plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With