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/
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.
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.
-Try using geopandas -Animation with matplotlib
Recommended Posts