python-2.7 - 更改 geopandas 中的单个补丁颜色

标签 python-2.7 pandas geopandas

使用this map of NYC我想把曼哈顿变成亮蓝色。但是,当我更改曼哈顿的单个补丁颜色时,所有其他补丁颜色也会发生变化。这出乎我的意料。

如何更改单个补丁的颜色?

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()

Output figure of NYBB

最佳答案

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

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

这给了我:

enter image description here

上述方法不起作用的原因是,geopandas 将第二个图添加到与第一个图相同的轴上(并且该轴是从 plot() 返回的)。因此,nybb_plotman_plot 引用的是同一个对象,因此您确实会第二次更新所有补丁。


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

关于python-2.7 - 更改 geopandas 中的单个补丁颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465889/

相关文章:

python - 在 geopandas 中转换为字符串时几何列被修剪

python - 将一列多边形从字符串转换为 GeoPandas 几何

python - 在 Python 中强制垃圾收集以释放内存

python正则表达式从极其复杂的成分字符串中解析数据

python - 根据条件计算组内共享元素的数量

python - Dataframe:为缺失日期添加新行

python - 如何合并 SQLAlchemy 和 postgresql 中的两个子查询

python - 音频文件到文本文件python

python - Pandas 在我的数据中按第一天重新采样

python - matplotlib 中每个图的宽度和高度轴相同