python - 类型错误 : ufunc 'sqrt' not supported for the input types when plotting a colormap in basemap

标签 python matplotlib-basemap 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()

控制台给出类型错误:


中的 TypeError Traceback(最近调用最后)
     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',根据转换规则 ''safe'' 无法将输入安全地强制转换为任何受支持的类型

我该如何解决这个问题? 最好的问候

最佳答案

plt.scatter 行中取出 'o'

关于python - 类型错误 : ufunc 'sqrt' not supported for the input types when plotting a colormap in basemap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56115201/

相关文章:

python - 在 numpy 中强制非数字字符为 NA(将 csv 读取到 pandas 数据帧时)

python - Ruby 相当于 Python 的 "dir"?

python - 用于绘制 map 的 basemap /项目的 Pandas 错误

animation - Python matplotlib : animation made of several pyplot. 图-s

python - 安装 basemap

c++ - 如何添加颜色图,OpenCV?

matlab - 如何通过Python获取3D彩色表面?

python - 根据列名替换值

python - Python的MemoryView底层机制

python - 根据另一个列表中的值创建相应的 RGB 列表(颜色图)