Last active
April 5, 2024 01:46
-
-
Save jjsantos01/af25c02ffa30acde8473385f08baf76f to your computer and use it in GitHub Desktop.
Urban evolution of Mexico City. Animation video at https://www.youtube.com/watch?v=VoqyihiVzxw
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 | |
import matplotlib.animation as animation | |
import time | |
start = time.time() | |
dir_datos = 'D:/datos/catastro_cdmx' | |
centroides = pd.read_pickle(f'{dir_datos}/centroides_completa.pkl') | |
fig, ax = plt.subplots(figsize=(15, 16.5)) | |
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None) | |
for s in ax.spines.values(): | |
s.set_visible(False) | |
xlims = (-99.37, -98.92) | |
ylims = (19.1, 19.6) | |
def animate(frame): | |
ax.cla() | |
year = frame | |
# plot año y | |
centroides.query('anio_construccion==@year').plot(markersize=0.1, figsize=(10, 10), color='green', label=str(year), ax=ax) | |
# plot años anteriores | |
centroides.query('anio_construccion<@year').plot(markersize=0.001, color='white', label=f'antes de {year}', ax=ax) | |
# Detalles de la gráfica | |
ax.set(aspect='equal', yticks=[], xticks=[], ylim=ylims, xlim=xlims, facecolor='k') | |
# titulo | |
ax.text(s=str(year), x=0.5, y=0.97, fontsize=20, color='white', transform=ax.transAxes) | |
# fuente | |
fuente = ax.text(s='Elaborado por @jjsantoso con datos de ADIP.', x=1, y=0, ha='right', color='white', fontsize=10, transform=ax.transAxes) | |
# Leyenda | |
ax.legend(loc='center right') | |
ley = ax.get_legend() | |
if ley: | |
for handle in ley.legendHandles: | |
handle.set_sizes([15]) | |
print(frame) | |
frames = list(range(1950, 2020)) | |
duracion = 30 # en segundos | |
interval = round(1000 * (duracion/len(frames)), 0) | |
ani = animation.FuncAnimation(fig, animate, frames=frames, blit=False, interval=interval) | |
ani.save("catastro_cdmx.mp4", dpi=200) | |
end = time.time() | |
print(end-start) | |
# plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment