Search code examples
pythonhistogram2d

ValueError: too many values to unpack (expected 2) when plotting a 2dhistogram


I am trying to plot a hist2d from a list of x,y coordinates but get a Value Error: too many values to unpack (expected 2).

The code is:

visuals = [[],[],[]]

with open('Wide_Single_timestamp2.csv') as csvfile :
    readCSV = csv.reader(csvfile, delimiter=',')
    n=0
    for row in readCSV :
        if n == 0 :
            n+=1
            continue
        visuals[0].append(list(map(float, row[2::2])))
        visuals[1].append(list(map(float, row[3::2])))

X = visuals[1]
Y = visuals[0]

fig, ax = plt.subplots(figsize = (8,7))
plt.grid(False)

data,x,y,p = plt.hist2d(X,Y, bins = 10, range = np.array([(-90, 90), (4, 140)]))
plt.imshow(data, interpolation = 'gaussian', origin = 'lower', extent = [-90,90,4,140], cmap = 'jet')

The code works when I reduce the list to a single row as such:

Y = visuals[1][0]
X = visuals[0][0]

But I need to plot more points of data.


Solution

  • Try using extend rather than append for extending a list by another list.

    That is, change the two lines:

        visuals[0].extend(list(map(float, row[2::2])))
        visuals[1].extend(list(map(float, row[3::2])))