我当前正在处理一些地理空间数据,并且我想在地图旁边添加一个颜色条。我使用以下代码在基线图上覆盖了散点图。但是,我不能在其旁边找到正确的颜色条。谁能找到这种行为的原因?
cm = plt.cm.get_cmap('RdYlBu') # define a colormap
fig,ax = plt.subplots(figsize=(15, 15))
city_map = df.plot(alpha=0.2, ax=ax) # plot a city contour
ctx.add_basemap(city_map) # plot a city map
points = gdf_new.plot(c=gdf_new['Score'], cmap=cm, ax=ax) # scatter plot
fig.colorbar(ax.collections[1], ax=ax) # current solution
pandas列gdf_new['Score']
包含用于点着色的从0到1的值。
如果将下面的最后一行替换为:
plt.colorbar()
-RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf)
plt.colorbar(point)
-AttributeError: 'AxesSubplot' object has no attribute 'get_array'
我当前的解决方案是添加:
fig.colorbar(ax.collections[1], ax=ax)
-它显示正确的颜色条,但数字范围为0-200,而不是0-1。
如何解决?