Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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.

share|improve this question
1  
Sir. Could provide to us the full stacktrace? – vyscond yesterday
1  
Please, show a sample of finaltheta.txt so I can use for my test. – flamenco yesterday

2 Answers 2

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.
share|improve this answer
    
I deduced the error is from loadtxt by removing everything away from the code, and just having a code that reads the data file. With that simple code, I have the same error. If it helps, the data file is just 3 columns, and I want to read the columns into their own array. – Nathan Melton 4 hours ago
    
Then there must be something unexpected in the data file. As you see, my test cases load 3 columns into 3 variables just fine. – hpaulj 4 hours ago
    
Why have't you shown us the calling stack with this error? How do you expect us to help you without more information? I'm tempted to close this question for incomplete information. – hpaulj 3 hours ago

I'm not completely sure but sequence would refer to x, y, c. Whereas the array is the file. If you really need three variables with the file I would give them their values separately.
x = np.loadtext('finaltheta.txt')
y = np.loadtext('finaltheta.txt')
c = np.loadtext('finaltheta.txt')
You would have to split it up in order for it to work.

share|improve this answer
    
Each of those calls does the same thing, read the whole file, create an array and assign it to a variable. – hpaulj 22 hours ago
    
In his code Nathan has three variables (x, y, c) that read the file and assign it to an array, and that is what is generating the error. I merely provided and explained a solution. – Plozzk 22 hours ago
    
'unpack=True' splits the output into several arrays, one per column. A mismatch between columns in the file and number of variables would produce an 'unpacking' error (same as a,b,c=1,2,3,4 would). – hpaulj 22 hours ago
    
According to my knowledge, the unpack=True statement assigns each column of the data file into the array. There are 3 columns in the text file, and I want each column in its own array. – Nathan Melton 4 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.