Python error - setting an array element with a sequence
Question: I have been trying to run the provided code to make a color map.

The data set has `x` and `y` coordinates, and each coordinate is to have it's
own color.

However, when I run the code, I get an error saying `setting an array element
with a sequence`.

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    from math import pi, sin
    
    x, y, c = np.loadtxt('finaltheta.txt', unpack = True)
    
    N = int(len(c)**0.5)
    c = c.reshape(N,N)
    
    plt.figure()
    plt.imshow(c, extent = (np.amin(x), np.amax(x), np.amin(y), np.amax(y)), cmap = cm.binary, vmin = 0.0, vmax = 1.0)
    cbar = plt.colorbar()
    plt.show()
    

I have deduced that the error is streaming from the `np.loadtxt` line.


Answer: What is the delimiter in the file?

I can simulate this kind of load with:

    
    
    In [223]: txt=b"1 2 3\n4 5 6".splitlines()
    In [224]: a,b,c=np.loadtxt(txt,unpack=True)
    In [225]: a
    Out[225]: array([ 1.,  4.])
    In [226]: b
    Out[226]: array([ 2.,  5.])
    In [227]: c
    Out[227]: array([ 3.,  6.])
    

Or with a , delimited text

    
    
    In [228]: txt=b"1,2,3\n4,5,6".splitlines()
    In [229]: a,b,c=np.loadtxt(txt,unpack=True,delimiter=',')
    

How do you deduce that the error is in the loadtxt? Normally an error gives
you a stacktrace that indicates clearly where the error occurs. It may be in
`loadtxt`, but if so the trace with show that it is indeed the loadtxt call.

I can't think, off hand, of a text that would produce this error in loadtxt.
The error means that something/someone is doing something like

    
    
    In [236]: a[0]=[1,2,3]
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-236-c47480f4cd6d> in <module>()
    ----> 1 a[0]=[1,2,3]
    
    ValueError: setting an array element with a sequence.
    

* * *

There's a scattering of SO questions with `loadtxt sequence`, e.g.

[CDF Cumulative Distribution Function
Error](http://stackoverflow.com/questions/25791634/cdf-cumulative-
distribution-function-error)

reports the error with stacktrace:

    
    
    Traceback (most recent call last):
      File "cum_graph.py", line 7, in <module>
        data = np.loadtxt('e_p_USC_30_days.txt')
      File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 804, in loadtxt
        X = np.array(X, dtype)
    ValueError: setting an array element with a sequence.
    

There is also a file sample. Unfortunately the accepted answer is just
flailing around trying to suggest a cause. I can't reproduce the error either.
That suggests that there are characters in the file that aren't reproduced in
the cutnpaste and/or the user has an older numpy version and/or there's an OS
issue (I'm on linux).

It would be intresting to see what the inputs to that line 804 look like, but
that would require a reproducable case, and then hacking loadtxt or running it
with the debugger.

I'd try `loadtxt` on a simpler, purpose built file, or try it on a portion of
the problem file. I'd also try `genfromtxt`. In most cases it does the
samething as load, but takes a sufficiently different approach that it could
bypass what ever problems loadtxt has.

