Geopandas and Geoplot¶
Below is a simple example of using geopandas and geoplot to visualize some spatial data. The data is derived from SMHI Open data and is downloaded and stored in GeoJSON by a Python script using Python bindings and utilities for GeoJSON.
Import libraries and ignore annoying warning message
%matplotlib inline
import matplotlib.pyplot as plt
import geopandas as gpd
import geoplot as gplt
import warnings; warnings.filterwarnings('ignore', 'GeoSeries.isna', UserWarning)
Read the shape file downloaded from Natural Earth. The file used is Admin 1 - States, Provinces. Then filter out Sweden.
world = gpd.read_file("data/ne_10m_admin_1_states_provinces.shp")
swe = world[world['admin'] == 'Sweden'] # Filter out Sweden from the world
Now read the data file on average daily temperatures from SMHI, use the coordinate reference system WGS84 ("epsg=4326") and display some data.
avg_temp = gpd.read_file("https://www.viltstigen.se/metobs/latest/02*") # Probl in EPSG 3006 or 3021
avg_temp = avg_temp.to_crs(epsg=4326)
avg_temp.head()
Equally, read some pressure data from the same date and display.
pressure = gpd.read_file("https://www.viltstigen.se/metobs/latest/09*")
pressure = pressure.to_crs(epsg=4326)
pressure.head()
Now do the plotting. Setup the projection, conic projection Albers. 2 plots will be made, for temperature and pressure, hence 2 axis is created.
Do a voronoi diagram for temperatures and pressure, then plot Sweden (including counties) on top. For the voronoi plts, use the colormap ("cmap") coolwarm, select the value as 'hue="value"`.
proj = gplt.crs.AlbersEqualArea()
fig = plt.figure(figsize=(10,5))
ax1 = plt.subplot(121, projection=proj)
ax2 = plt.subplot(122, projection=proj)
gplt.voronoi(avg_temp,
cmap='coolwarm',
clip=swe,
hue="value",
legend=True,
edgecolor="None",
projection=proj,
ax=ax1)
gplt.polyplot(swe, edgecolor="Black", zorder=1, linewidth=0.5, projection=proj, ax=ax1)
gplt.voronoi(pressure,
cmap='coolwarm',
clip=swe,
hue="value",
legend=True,
edgecolor="None",
projection=proj,
ax=ax2)
gplt.polyplot(swe, edgecolor="Black", zorder=1, linewidth=0.5, projection=proj, ax=ax2)
That's it