Last active
October 14, 2022 21:54
-
-
Save rohithteja/058a75d6f06196ab145b0153e5f1fde0 to your computer and use it in GitHub Desktop.
Choropleth geopandas
This file contains 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
import pandas as pd | |
import geopandas as gpd | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.axes_grid1 import make_axes_locatable | |
# read data | |
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv') | |
df.rename(columns={'code':'STUSPS'}, inplace=True) | |
# 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_state_map = pd.merge(df,state_map,on='STUSPS') | |
df_state_map = gpd.GeoDataFrame(df_state_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_state_map.plot(ax=ax,cax=cax, column='total exports', cmap='Oranges', legend=True, edgecolor='black', linewidth=0.5,legend_kwds={'shrink':0.6}) | |
ax.set_axis_off() | |
# label the colorbar | |
cax.tick_params(labelsize=10) | |
cax.set_ylabel('Total Exports',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