Animation of geographic data by geopandas

Introduction

geopandas is an extension of pandas, a python library that can handle data including geographic data in tabular format like pandas. geopandas also includes features for geodata visualization. However, since the data is just passed to matplotlib as it is, basically you should be able to create animations with matplotlib. However, the current stable version of geopandas, 0.2.1, makes it difficult to access the artist object. Therefore, if you want to animate, it is recommended to install from the latest source code of github as follows until the next stable version is released.

pip install -U git+https://github.com/geopandas/geopandas/

Data to use

This time, we will visualize the transition of the population ratio in the 23 wards of Tokyo (for geographic data, it may be more difficult to prepare the data than to actually process it).

Topographic data is from JapanCityGeoJson 2016 to tokyo23.json I'm downloading a geojson file called).

Population data is [Wikipedia: Tokyo 23 wards](https://ja.wikipedia.org/wiki/%E6%9D%B1%E4%BA%AC%E9%83%BD%E5%8C%BA%E9 I created a file that reconstructed the data of% 83% A8) with the name 23population.csv.

animation

Now let's actually execute the animation. Below is the code.

import geopandas as gpd

df = pd.read_csv('23population.csv')
geodf = gpd.read_file('tokyo23.json').dissolve(by="id").sort_index()
years = np.sort(np.unique(df.year.values))

Population data is loaded into a variable called df, and terrain data is loaded into a variable called geodf.

from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ims = []
def update_fig(year):
    if len(ims) > 0:
        ims[0].remove()
        del ims[0]
    geos = geodf['geometry'].values
    sum = np.sum(df[(df.year==year)].sort_values(by='city').population)
    population_rates = df[(df.year==year)].sort_values(by='city').population/sum
    artist = gpd.plotting.plot_polygon_collection(ax, geos, population_rates, True, cmap="Reds")
    ims.append(artist)
    ax.set_title('year = ' + str(year))
    return ims
anim = FuncAnimation(fig, update_fig, interval=1000, repeat_delay=3000, frames=years)
fig.show()

To get the artist object, drawing the terrain data calls the function gpd.plotting.plot_polygon_collection called in it instead of gpd.GeoDataFrame.plot.

tokyo23.gif

Reference link

-Try using geopandas -Animation with matplotlib

Recommended Posts

Animation of geographic data by geopandas
Visualization of data by prefecture
Sentiment analysis of large-scale tweet data by NLTK
Numerical summary of data
Analysis of financial data by pandas and its visualization (2)
Anomaly detection of time series data by LSTM (Keras)
Split data by threshold
Training data by CNN
Correlation by data preprocessing
Visualization method of data by explanatory variable and objective variable
Preprocessing of prefecture data
Selection of measurement data
What I saw by analyzing the data of the engineer market
Tuning experiment of Tensorflow data
Classify data by k-means method
Gzip the data by streaming
Calculation of similarity by MinHash
Fourier transform of raw data
Data acquired by Django releted
Average estimation of capped data
About data management of anvil-app-server
Probability prediction of imbalanced data
Automatic acquisition of gene expression level data by python and R
Impressions of touching Dash, a data visualization tool made by python