首页 > 解决方案 > 将颜色条添加到散点图

问题描述

我目前正在处理一些地理空间数据,我想在地图旁边添加一个颜色条。我使用以下代码用散点图覆盖基线图。但是,我无法在它旁边找到正确的颜色条。谁能找到这种行为的原因?

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。

如何解决?

标签: pythonmatplotlibscatter-plot

解决方案


推荐阅读