【问题标题】:How to show both colormaps in the legend in GeoPandas如何在 GeoPandas 的图例中显示两个颜色图
【发布时间】:2020-11-10 07:04:11
【问题描述】:

我正在尝试绘制加利福尼亚州代表错过选票的百分比,但要注意根据政党进行着色。我可以在同一个图上使用 GeoPandas 绘制两个颜色图,但我无法让两个颜色图都显示在图例上。

fig, ax = plt.subplots(figsize=(10, 6))

california = merged[merged['STATENAME'] == 'California']

rs = california[california['party'] == 'R']
ds = california[california['party'] == 'D']
# set the value column that will be visualised
variable = 'PCT'
# create map
rs.plot(column=variable, ax=ax, legend=False, cmap='Reds', scheme='quantiles', k=7)
ds.plot(column=variable, ax=ax, legend=True, cmap='Blues', scheme='quantiles', k=7)

生成的图如下所示:

【问题讨论】:

  • 为什么你在 rs 图上有 legend=False?你能提供给我们数据吗?
  • 能否将 legend=False 更改为 legend=True 并重新运行脚本。
  • @squeezer44 将两行都设置为 Legend=True 不会改变任何东西。当我发布此内容时,我正在使用其中一个或另一个来查看它是否可以工作,但我忘记了将其更改回来。这是我用来生成加利福尼亚图的示例数据:drive.google.com/file/d/1lp5CeMyC88cFBRRxfOmAevo6UHeDU0za/…

标签: python pandas matplotlib geopandas


【解决方案1】:

关键是确保您对两个图例对象都有引用:
在您的示例中,您实例化的第一个(并显示),第二个您没有实例化(未显示)。

一般意思是:

# Add first legend: only labeled data is included
leg1 = ax.legend(loc='upper right')

# Add second legend
# leg1 will be removed from figure
leg2 = ax.legend(loc='lower left')

# Manually add the first legend back
ax.add_artist(leg1)

ByTheWay:您的代码示例不完整:缺少导入和转换为 GeoDataFrame。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多