【问题标题】:Update patch edge colours in Geopandas plot更新 Geopandas 图中的补丁边缘颜色
【发布时间】:2022-04-22 01:19:50
【问题描述】:

我使用以下代码(geopandas 0.2.1,matplotlib 2.0.2,在 Jupyter 笔记本中,使用 %inline

fig, ax = plt.subplots(
    1,
    figsize=(16., 12.),
    dpi=100,
    subplot_kw=dict(aspect='equal'),
)

base = imd_gdf.dropna().plot(
    ax=ax,
    alpha=1.,
    column="Rental_Count",
    scheme="fisher_jenks",
    k=7,
    cmap="viridis",
    linewidth=0.1,
    edgecolor='black',
    legend=True,
)

这给了我一个多边形周围有边缘的地图:

我想删除这些。到目前为止,我已经尝试在补丁中循环,将边缘颜色设置为面部颜色:

for p in ax.patches:
    nc = p.get_facecolor()
    p.set_edgecolor(nc) 

但它没有任何效果,即使我在循环中指定了一种颜色。 p.set_color(nc) 或尝试使用 p.set_linewidth(0.0) 将线宽设置为 0 都没有任何效果。 我在这里想念什么?使用p.set_facecolor('white') 以相同的方式更新面部颜色效果很好:

【问题讨论】:

  • 在对plot 方法的原始调用中将边缘颜色设置为字符串"none" 时会发生什么?
  • @PaulH 将其设置为 'none' 会导致边缘被绘制为白色

标签: python pandas matplotlib geopandas


【解决方案1】:

所以事实证明plot() 绘制了一组线对象(AxesSubplotlines 属性,它们是lines.Line2D),它们代表了实际的边。如果您想完全控制边缘外观,则需要更新这些以及补丁的边缘属性 (AxesSubplot.patches)。

2022 年 4 月更新

较新的 Geopandas 版本 (0.10+) 有一个额外的 missing_kwds dict 可以传递给 plot() 函数,以便在绘制时绘制具有给定输入 column 参数的 NaN 值的几何图形一个等值线。这会导致绘制一个新的子PatchCollection,并且它总是(在这个版本中,这可能会改变吗?)ax._children第二个子。

为了修改这两个单独的PatchCollections 的绘制方式,您必须执行以下操作:

fig, ax = plt.subplots(..., missing_kwds={facecolor='#000000'}, ...)
df.plot(...)
# NaN polygons
c = ax._children[1]
c.set_edgecolors(#000000) # match the facecolor in missing_kwds
c.set_zorder(1) # this ensures it's drawn last

# Drawn polygons
c2 = ax._children[0]
c2.set_edgecolors(retained_ec)
c2.set_zorder(2) # this ensures it's drawn first

# now call plt.savefig, plt.show etc

【讨论】:

  • 如何访问lines.Line2D?谢谢
  • @MorningGlory 不知道你是否知道,但我发布了一个更新,如果没有,可能会有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 2017-01-07
  • 2013-08-26
  • 1970-01-01
相关资源
最近更新 更多