Last active
November 1, 2022 10:43
-
-
Save rohithteja/74885d82cceed9bc0cb1d63240022827 to your computer and use it in GitHub Desktop.
choropleth map
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 plotly.express as px | |
import imageio | |
# removing small countries and NA values | |
geo = shapefile.dropna() | |
geo = geo.reset_index(drop=True) | |
geo = geo[['iso3','name','geometry']] | |
# appending temperature values to geo DF | |
for i in range(df_temp.shape[0]): | |
dic = dict(df_temp.iloc[i]) | |
geo[idx[i]] = geo['name'].map(dic) | |
# converting the temperature columns into rows | |
geo = geo.melt(id_vars=["iso3", "name","geometry"], | |
var_name="Date", | |
value_name="Temperature (deg C)") | |
# choropleth maps on a loop | |
for i in idx: | |
temp = geo[geo['Date']==i] | |
fig = px.choropleth(temp, | |
geojson=temp.geometry, | |
locations=temp.index, | |
color='Temperature (deg C)', | |
color_continuous_scale='RdYlBu_r', | |
range_color=(-30, 40), | |
width=1000, height=500) | |
fig.update_layout(title=f'Average Country Temperatures ({i} 2021)', | |
title_x=0.5, | |
margin=dict(l=20, r=20, t=40, b=20)) | |
fig.write_image(f"world-{i}.png", scale=2) | |
# creating a GIF | |
images = [] | |
for filename in [f"world-{i}.png" for i in idx]: | |
images.append(imageio.imread(filename)) | |
imageio.mimsave('mygif.gif', images,duration=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment