Skip to content

Instantly share code, notes, and snippets.

@rohithteja
Created October 14, 2022 12:52
Show Gist options
  • Save rohithteja/4e745ebb66149d8894bea5fdfa358118 to your computer and use it in GitHub Desktop.
Save rohithteja/4e745ebb66149d8894bea5fdfa358118 to your computer and use it in GitHub Desktop.
Choropleth plotly express
import pandas as pd
import geopandas as gpd
import plotly.express as px
# read data
df = pd.read_csv('data/population.csv',header=2)
df.rename(columns={'Country Code':'iso3'}, inplace=True)
# read shapefile
map = gpd.read_file('data/world-administrative-boundaries/world-administrative-boundaries.shp')
# merge df and shapefile
df_map = pd.merge(df,map,on='iso3')
# plot
fig = px.choropleth(df_map, locations='iso3', color='2021',
color_continuous_scale='Reds',
scope='world',projection='equirectangular')
fig.update_layout(margin={'r':0,'t':0,'l':0,'b':0})
fig.update_geos(resolution=50,
showcoastlines=True, coastlinecolor='black',
showland=True, landcolor='#e6e6e6',
showlakes=True, lakecolor='LightBlue')
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment