. Advertisement .
..3..
. Advertisement .
..4..
I get an 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
when I try to run the following code:
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()
How to fix the indexerror: arrays used as indices must be of integer (or boolean) type. Please give me some good ideas.
The cause:
After hours of thinking I realize that the values which are extracted in your program are not integers, they are the ‘float’ type. However, if an array is used as the value of indices, it only accept int type or boolean type but don’t accept any other data type.
This is the reason why the error: ”indexerror: arrays used as indices must be of integer (or boolean) type” happens.
Solution:
Solving this error is very simply. You only need to change the float type into int type for the array as following:
Your program will run well without any error.
The problem was solved by simply converting the floating array to an int array.