更改Geopandas中的单个补丁颜色 [英] Change single patch color in geopandas

查看:187
本文介绍了更改Geopandas中的单个补丁颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这张纽约市地图将曼哈顿更改为明亮的蓝色.但是,当我更改曼哈顿的单独色块颜色时,所有其他色块颜色也会更改.这对我来说是出乎意料的.

Using this map of NYC I'd like to change Manhattan to be bright blue. But when I change the individual patch color of Manhattan all the other patch colors change too. This was unexpected to me.

如何更改单个贴片的颜色?

How do you change the color of one individual patch?

from matplotlib import pyplot as plt
import geopandas as gpd
nybb = gpd.GeoDataFrame.from_file('nybb.shp')


nybb_plot = nybb.plot()
for p_ny in nybb_plot.patches:
    p_ny.set_color("#111111")
    p_ny.set_alpha(0.6)

for line in nybb_plot.lines:
    line.set_linewidth(0.25)
    line.set_alpha(0.9)
    line.set_color("#d3d3d3")

manhattan = nybb.loc[nybb.BoroName == "Manhattan"]

man_plot = manhattan.plot()
for p_mh in man_plot.patches:
    p_mh.set_color("#33ccff")

plt.show()

推荐答案

一种可行的解决方案是使用geopandas.plotting.plot_multipolygon专门在现有图形上仅添加一个带有蓝色的几何对象:

A possible solution is using geopandas.plotting.plot_multipolygon to specifically add only one geometry object with blue colors to the existing figure:

from geopandas.plotting import plot_multipolygon
manhattan = nybb[nybb.BoroName == "Manhattan"]
plot_multipolygon(nybb_plot, manhattan.geometry.iloc[0], facecolor="#33ccff", edgecolor='none')

这给了我

您的上述方法无效的原因是,geopandas将第二个图添加到与第一个图相同的轴上(并且该轴从plot()返回).因此,nybb_plotman_plot指的是同一个对象,因此您第二次确实更新了所有补丁.

The reason your above approach does not work, is because geopandas adds the second plot to the same axes as the first plot (and this axes is returned from plot()). So nybb_plot and man_plot are referring to the same object, and so you do update all patches the second time.

请注意,在开发版本中,第二个绘图将不再自动添加到第一个绘图中,而是会创建一个新图形.

Note that in the development version, the second plot will not automatically added anymore to the first, but a new figure will be created.

这篇关于更改Geopandas中的单个补丁颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆