Reputation: 73
I found this website to get geographical data per country: https://gadm.org/ .
Now I want to import the data from the Netherlands in python and plot my numerical data per province/city in that geographical map.
Someone who has experience with data from this site? Or with other data for geographical plotting in certain countries?
I don't succeed loading this data into Python.
Upvotes: 1
Views: 2711
Reputation: 3825
Well I ran into the same issue so here is how I solved a similar problem using matplotlib
, pandas
and geopandas
. Also some inspiration from this article: https://towardsdatascience.com/lets-make-a-map-using-geopandas-pandas-and-matplotlib-to-make-a-chloropleth-map-dddc31c1983d
First I downloaded a shapefile of the dutch provinces (if you want other regions, e.g. communes, find a shapefile of those). I used this one: https://maps.princeton.edu/catalog/stanford-st293bj4601, the direct link to the download is: https://stacks.stanford.edu/file/druid:st293bj4601/data.zip
I opened it like this:
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
mapdf = gpd.read_file("https://stacks.stanford.edu/file/druid:st293bj4601/data.zip")
This map contains provinces and bodies of water. I only kept the provinces since I dont care about the bodies of water, and I also sorted the data on the province names
mapdf = mapdf[mapdf["TYPE_1"] == "Provincie"]
mapdf.sort_values("NAME_1", inplace=True)
mapdf = mapdf.reset_index(drop=True)
So this is what my data sumdf
looks like:
I made sure the provinces are in the same order as in the mapdf
Now that they have the same length and are in the same order I can concatenate. If for some reason you cant easily get your data in the same order, then maybe look on joining it or using some other way to combine them correctly.
I combine them
mapdf = gpd.GeoDataFrame(pd.concat([sumdf, mapdf], axis=1))
and get
You can see the column names, we now have both data in one dataframe
.
Now I can plot my data:
mapdf.plot(column="BUY", figsize=(10,10), legend=True)
Upvotes: 1