Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python,IndexError: arrays used as indices must be of integer (or boolean) type

Iam getting an IndexError: arrays used as indices must be of integer (or boolean) type at the line for pcolormesh, any idea how to handle this.

script:

import numpy as np 
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

data = np.loadtxt('out (copie).txt')
lats = data[:,0]
lons = data[:,1]
codg_tec = data[:,2]

m = Basemap(projection = 'merc', llcrnrlon= -9, llcrnrlat=19, urcrnrlon= 12, urcrnrlat= 37,  resolution= 'i')
m.drawcoastlines()

lon, lat = np.meshgrid(lons, lats)
x, y = m(lon, lat)

cb = m.pcolormesh(x, y, np.squeeze(data[codg_tec]) , shading='flat', cmap=plt.cm.jet)
cbar = m.colorbar(cb, location = 'right', pad = '10%')

m.drawmapboundary()
m.drawmapscale()
m.drawmeridians(np.arange(-9,12,5), labels=[False,False,False,True])
m.drawparallels(np.arange(19,38,5), labels=[True,False,False,False])
m.drawstates()
m.drawcountries()

plt.title('CODG-vTEC on 02-01-2015')
plt.show() 

The error:

Traceback (most recent call last):
 File "color.py", line 21, in <module>
  cb = m.pcolor(x, y, data[codg_tec] , shading='flat', cmap=plt.cm.jet)
IndexError: arrays used as indices must be of integer (or boolean) type
like image 896
Bachi Avatar asked Jun 11 '20 16:06

Bachi


1 Answers

the problem was solved just by converting the float array into int array:

lats = data[:,0].astype(int)
lons = data[:,1].astype(int)
codg_tec = data[:,2].astype(int)
like image 124
Bachi Avatar answered Oct 22 '22 15:10

Bachi