From 27af215f3d265e603a18fc663ba2a9cd7e551b8a Mon Sep 17 00:00:00 2001 From: Dani Arribas-Bel Date: Thu, 1 Aug 2019 17:35:18 +0100 Subject: [PATCH 1/4] Adding wrapping of text --- contextily/plotting.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/contextily/plotting.py b/contextily/plotting.py index 17753145..8b61823d 100644 --- a/contextily/plotting.py +++ b/contextily/plotting.py @@ -181,11 +181,15 @@ def add_attribution(ax, att=ATTRIBUTION, font_size=ATTRIBUTION_SIZE): """ minX, maxX = ax.get_xlim() minY, maxY = ax.get_ylim() - ax.text( - minX + (maxX - minX) * 0.005, - minY + (maxY - minY) * 0.005, - att, - size=font_size, - path_effects=[patheffects.withStroke(linewidth=2, foreground="w")], - ) + txt = ax.text( + minX + (maxX - minX) * 0.005, + minY + (maxY - minY) * 0.005, + att, + size=font_size, + path_effects=[patheffects.withStroke(linewidth=2, foreground="w")], + wrap=True + ) + bb = ax.get_window_extent() + wrap_width = (bb.x1 - bb.x0) - (bb.x1 - bb.x0) * 0.1 + txt._get_wrap_line_width = lambda : wrap_width return ax From d508c981edbfa0ba4ee26f013c9e3ab60d77b692 Mon Sep 17 00:00:00 2001 From: Dani Arribas-Bel Date: Thu, 1 Aug 2019 17:37:34 +0100 Subject: [PATCH 2/4] Blacking commit --- contextily/plotting.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contextily/plotting.py b/contextily/plotting.py index 8b61823d..f3fdce75 100644 --- a/contextily/plotting.py +++ b/contextily/plotting.py @@ -182,14 +182,14 @@ def add_attribution(ax, att=ATTRIBUTION, font_size=ATTRIBUTION_SIZE): minX, maxX = ax.get_xlim() minY, maxY = ax.get_ylim() txt = ax.text( - minX + (maxX - minX) * 0.005, - minY + (maxY - minY) * 0.005, - att, - size=font_size, - path_effects=[patheffects.withStroke(linewidth=2, foreground="w")], - wrap=True - ) + minX + (maxX - minX) * 0.005, + minY + (maxY - minY) * 0.005, + att, + size=font_size, + path_effects=[patheffects.withStroke(linewidth=2, foreground="w")], + wrap=True, + ) bb = ax.get_window_extent() wrap_width = (bb.x1 - bb.x0) - (bb.x1 - bb.x0) * 0.1 - txt._get_wrap_line_width = lambda : wrap_width + txt._get_wrap_line_width = lambda: wrap_width return ax From fcca46e7bb0aea5c60ff5afa384e7b081bb0e453 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 1 Aug 2019 20:10:15 +0200 Subject: [PATCH 3/4] small update --- contextily/plotting.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contextily/plotting.py b/contextily/plotting.py index f3fdce75..f03bf2bb 100644 --- a/contextily/plotting.py +++ b/contextily/plotting.py @@ -189,7 +189,8 @@ def add_attribution(ax, att=ATTRIBUTION, font_size=ATTRIBUTION_SIZE): path_effects=[patheffects.withStroke(linewidth=2, foreground="w")], wrap=True, ) - bb = ax.get_window_extent() - wrap_width = (bb.x1 - bb.x0) - (bb.x1 - bb.x0) * 0.1 + # hack to have the text wrapped in the ax extent, for some explanation see + # https://stackoverflow.com/questions/48079364/wrapping-text-not-working-in-matplotlib + wrap_width = ax.get_window_extent().width * 0.99 txt._get_wrap_line_width = lambda: wrap_width return ax From 66ae705908cb0b60467c44ee07715bf91782617f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 1 Aug 2019 20:15:34 +0200 Subject: [PATCH 4/4] use relative positioning --- contextily/plotting.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/contextily/plotting.py b/contextily/plotting.py index f03bf2bb..da3ce03d 100644 --- a/contextily/plotting.py +++ b/contextily/plotting.py @@ -179,12 +179,11 @@ def add_attribution(ax, att=ATTRIBUTION, font_size=ATTRIBUTION_SIZE): Matplotlib axis with `x_lim` and `y_lim` set in Web Mercator (EPSG=3857) and attribution text added """ - minX, maxX = ax.get_xlim() - minY, maxY = ax.get_ylim() txt = ax.text( - minX + (maxX - minX) * 0.005, - minY + (maxY - minY) * 0.005, + 0.005, + 0.005, att, + transform=ax.transAxes, size=font_size, path_effects=[patheffects.withStroke(linewidth=2, foreground="w")], wrap=True,