python - 将颜色条添加到散点图
问题描述
我目前正在处理一些地理空间数据,我想在地图旁边添加一个颜色条。我使用以下代码用散点图覆盖基线图。但是,我无法在它旁边找到正确的颜色条。谁能找到这种行为的原因?
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。
如何解决?
解决方案
推荐阅读
- c# - NuGet:复制 .DLL 文件而不创建引用?
- android - 为什么我无法在 android 10 中打开文件选择器并显示 toast 无法创建图像文件
- php - 在 wordpress 中创建自定义表单
- php - PHP 通过动态传递值来发布请求
- java - 线程“主”java.net.UnknownHostException 中的异常:repo.maven.apache.org
- sql - SQL Server 基于另一个表替换字符串
- c# - 如何使用单个表批量更新 sqlite 数据库
- php - 比较两个多维和关联数组并获得差异
- android - 显示异常的地方自动完成 api
- excel - 从一张纸到另一张纸的正确引用,没有#ref