From 6344bc13827c7036d2abb9fb788456e65f8149a3 Mon Sep 17 00:00:00 2001 From: Lukas Grossberger Date: Fri, 11 Nov 2022 23:02:53 +0100 Subject: [PATCH] switch to matplotlib and add bundesland borders --- main.py | 52 +++++++++++++++++++++++++++--------------------- requirements.txt | 2 +- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index f37738a..ab32437 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,9 @@ import argparse import json -import cartopy.crs as ccrs import geopandas as gpd -import geoplot -import geoplot.crs as gcrs import matplotlib.pyplot as plt +import numpy as np def main( @@ -29,33 +27,41 @@ def main( for plz_prefix in plz_prefixes: data.loc[data["plz"].str.startswith(plz_prefix), "highlighted"] = True - ax = plt.axes(projection=ccrs.TransverseMercator()) - geoplot.polyplot( - data, - projection=gcrs.TransverseMercator(), - edgecolor=map_background_color, - facecolor=map_background_color, - linewidth=0.3, - ax=ax, - ) - geoplot.polyplot( - data[data["highlighted"]], - projection=gcrs.TransverseMercator(), - edgecolor=map_accent_color, - facecolor=map_accent_color, - linewidth=0.3, - ax=ax, - ) + # TODO: This should instead be a proper geo spatial projection + coordinate_transform = np.array([[1.0, 1.6]]).T + + ax = plt.axes(aspect="equal") + for bundesland in data["bundesland"].unique(): + bundesland_df = data[data["bundesland"] == bundesland] + union = bundesland_df.unary_union + for geom in getattr(union, "geoms", [union]): + ax.fill( + *(geom.exterior.xy * coordinate_transform), + fc=map_background_color, + ec=image_background_color, + linewidth=0.1, + ) + + if not any(bundesland_df["highlighted"]): + continue + + union = bundesland_df[bundesland_df["highlighted"]].unary_union + for geom in getattr(union, "geoms", [union]): + ax.fill( + *(geom.exterior.xy * coordinate_transform), + fc=map_accent_color, + ec=None, + ) ax.set_facecolor(image_background_color) plt.axis("off") - plt.tight_layout(pad=0.0, h_pad=None, w_pad=None) + plt.tight_layout(pad=0.0, h_pad=0.0, w_pad=0.0) plt.savefig( output_file_path, - dpi=600, + dpi=300, facecolor=image_background_color, edgecolor="none", - bbox_inches='tight', + bbox_inches="tight", ) diff --git a/requirements.txt b/requirements.txt index ae69ef6..e290df5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ geopandas==0.11.1 -geoplot==0.5.1 +matplotlib==3.6.2 \ No newline at end of file