I would like to draw a scatter plot about the relationship between house prices and geographic coordinates using a colormap.

from mpl_toolkits.basemap import Basemap


lats = df['latitude'].as_matrix()
lons = df['longitude'].as_matrix()

zoom_scale = 0

bbox = [np.min(lats)-zoom_scale,np.max(lats)+zoom_scale,\
        np.min(lons)-zoom_scale,np.max(lons)+zoom_scale]

plt.figure()

m = Basemap(projection='merc',llcrnrlat=bbox[0],urcrnrlat=bbox[1],\
    llcrnrlon=bbox[2],urcrnrlon=bbox[3],resolution='i')

m.drawcoastlines()
m.drawrivers()

x,y = m(lons,lats)
cm = plt.cm.get_cmap('coolwarm')
plot = m.scatter(x,y,'o',c = df['price'],cmap=cm)
plt.colorbar(plot)
plt.title("House Price with Geographic Coordinate")
plt.show()

The console gives a TypeError:


TypeError Traceback (most recent call last) in

     28 x,y = m(lons,lats)
     29 cm = plt.cm.get_cmap('coolwarm')
---> 30 plot = m.scatter(x,y,'o',c = df['price'],cmap=cm)
     31 plt.colorbar(plot)
     32 plt.title("House Price with Geographic Coordinate")

....

/anaconda3/lib/python3.7/site-packages/matplotlib/collections.py in set_sizes(self, sizes, dpi)
    872             self._sizes = np.asarray(sizes)
    873             self._transforms = np.zeros((len(self._sizes), 3, 3))
--> 874             scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
    875             self._transforms[:, 0, 0] = scale
    876             self._transforms[:, 1, 1] = scale

TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

How can I solve this? Best regards

0 Answers