Skip to content

Instantly share code, notes, and snippets.

@jjsantos01
Last active April 5, 2024 01:46
Show Gist options
  • Save jjsantos01/af25c02ffa30acde8473385f08baf76f to your computer and use it in GitHub Desktop.
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
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()
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment