简体   繁体   中英

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.

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])))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM