Change single patch color in geopandas

北城以北 提交于 2020-01-02 11:01:22

问题


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


回答1:


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

This gives me:

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.



来源:https://stackoverflow.com/questions/32465889/change-single-patch-color-in-geopandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!