[AstroPy] question on matplotlib's loadtxt
Miguel de Val-Borro
miguel.deval at gmail.com
Fri Nov 4 10:56:16 EDT 2011
Hi Grigoris,
On Fri, Nov 04, 2011 at 03:56:21PM +0200, Grigoris Maravelias wrote:
> Suppose that we have a file (called "test.test") with 6 columns like:
>
> 1496548. 862.235400937 14.008 0 20110523.201416 tth*g
> 1690289. 919.424007603 13.875 0 20110523.221527 hgf4
> 1667241. 996.262754039 13.890 0 20110524.001639 nb.mj
> 739181.6 881.1753527 14.774 0 20110524.010203 vbfhg
>
> When I use
> x, y = loadtxt('test.test', unpack=True, usecols=(0,1), dtype=(float,float))
>
> it prints normally the first and second column.
> If I add another column like:
>
> x, y, z = loadtxt('test.test', unpack=True,usecols=(0,1,3),
> dtype=(float,float,float))
This seems to work because the default dtype in loadtxt is float:
x, y, z = numpy.loadtxt('test.test', unpack=True,usecols=(0,1,3))
> Additionaly, when I am trying to load the last column as strings:
> x, y = loadtxt('test.test', delimiter=' ', unpack=True,usecols=(0,5),
> dtype=(float,'S5'))
>
> I get this error:
> File "/usr/lib/python2.7/site-packages/numpy/lib/io.py", line 584, in
> loadtxt
> dtype = np.dtype(dtype)
> ValueError: mismatch in size of old and new data-descriptor
I'm not sure about this error message. I suggest that you try
numpy.genfromtxt which is a very convenient function to read text data
from a file into a recarray. There are several examples with mixed data
types in the documentation. For example specifying the dtypes:
data = numpy.genfromtxt('test.test', dtype="f8,f8,f8,f8,f8,S5")
Then the last column could be accessed as data['f5']
Miguel
More information about the AstroPy
mailing list