GeoPandas, MatPlotLib Plot Custom Colors

[亡魂溺海] 提交于 2019-12-03 08:23:42

Geopandas wants to color your map according to data in your geopandas dataframe. So the simplest coloring scheme you could go with is to add a column 'color' to your dataframe and populate it with some values based on how you want your counties colored.

import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

shpfile = 'cb_2015_us_county_20m.shp'
c = gpd.read_file(shpfile)
c = c.loc[c['GEOID'].isin(['26161','26093','26049','26091','26075',
                           '26125','26163','26099','26115','26065'])]

c['color'] = np.zeros(len(c))
# 23 is index for Washtenaw county and 1992 is index for Wayne county
c.ix[23, 'color'] = 1.0
c.ix[1992, 'color'] = 1.0

# create simple linear colormap that maps grey to blue
cmap = LinearSegmentedColormap.from_list(
    'mycmap', [(0, 'grey'), (1, 'blue')])

c.plot(column='color', cmap=cmap)

Perhaps it's not the most elegant solution, but this should at least explain the concept of how colormaps function in geopandas and get you the plot you're looking for. Also check out this page of the geopandas docs for a little more info on map coloring.

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