Created
October 14, 2022 22:34
-
-
Save rohithteja/c064a5c07783e965d8eabd0cb25a8415 to your computer and use it in GitHub Desktop.
Choropleth Geopandas County
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str}) | |
df.rename(columns={'fips':'GEOID'}, inplace=True) | |
# read US county shapefile | |
county_map = gpd.read_file('data/cb_2018_us_county_500k/cb_2018_us_county_500k.shp') | |
county_map = county_map[~county_map.STATEFP.isin(['02','15','72','60','66','69','78','11'])] | |
county_map = county_map.to_crs("EPSG:2163") | |
# read US state shapefile | |
state_map = gpd.read_file('data/cb_2018_us_state_500k/cb_2018_us_state_500k.shp') | |
state_map = state_map[~state_map.STATEFP.isin(['02','15','72','60','66','69','78','11'])] | |
# fixing projection | |
state_map = state_map.to_crs('EPSG:2163') | |
# merging datasets | |
df_county_map = pd.merge(df,county_map,on='GEOID') | |
df_county_map = gpd.GeoDataFrame(df_county_map) | |
fig, ax = plt.subplots(1, figsize=(10, 6),dpi=300,facecolor='w',edgecolor='w') | |
# colorbar | |
divider = make_axes_locatable(ax) | |
cax = divider.append_axes('right', size='5%', pad=0.1) | |
#plot | |
gax = df_county_map.plot(ax=ax,cax=cax, column='unemp', cmap='Reds', legend=True, edgecolor='grey', linewidth=0.5,legend_kwds={'shrink':0.6}) | |
# plot state boundaries | |
state_map.boundary.plot(ax=gax, color='black', linewidth=0.5,zorder=1) | |
ax.set_axis_off() | |
# label the colorbar | |
cax.tick_params(labelsize=10) | |
cax.set_ylabel('Unemployment Rate',fontsize=10) | |
cax.yaxis.set_label_position('left') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment