4

I created a map using geopandas, but I am unable to add a "North Arrow" on the map.

After creating the map, I have tried to add the "north arrow" using matplotlib.image module and tried different ways (see example below) but none of them provided a good result. I am looking for better code that can add a good "North Arrow to the map"

import matplotlib.image as img

from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, 
    AnnotationBbox

im=img.imread(r'C:\Users\jnisengw\Dropbox\2019\Data 
    Science\QGIS\north_arrow1.png')

imagebox = OffsetImage(im,zoom=0.27)

ab = AnnotationBbox(imagebox, (598500,4699000))

ax.add_artist(ab)

steven
  • 1,788
  • 13
  • 35
Regis
  • 41
  • 1
  • 3
  • We can't see your image. You need to post the image here (not a third-party link, Dropbox, etc.) – smci Sep 24 '19 at 22:38
  • similar technique as this https://stackoverflow.com/questions/34458251/plot-over-an-image-background-in-python/34459284#34459284 – Paul H Sep 25 '19 at 23:13
  • https://www.net-analysis.com/blog/cartopylayout.html – steven Sep 26 '19 at 03:21

2 Answers2

10

If you only need to add a simple arrow, you can also consider the annotate() method.

import geopandas as gpd
import matplotlib.pyplot as plt

gdf = gpd.read_file(gpd.datasets.get_path('nybb'))

fig, ax = plt.subplots(figsize=(6, 6))
gdf.plot(ax=ax)

x, y, arrow_length = 0.5, 0.5, 0.1
ax.annotate('N', xy=(x, y), xytext=(x, y-arrow_length),
            arrowprops=dict(facecolor='black', width=5, headwidth=15),
            ha='center', va='center', fontsize=20,
            xycoords=ax.transAxes)

enter image description here

Notes: When you pass xycoords=ax.transAxes, the x, y coordinate is normalized, and x, y = 0.5, 0.5 means you put the arrowhead in the middle of your map.

steven
  • 1,788
  • 13
  • 35
0

In case somebody still needs this...
EOmaps v3.1 now has a proper north-arrow and interacts nicely with geopandas!

from eomaps import Maps
m = Maps()
m.add_feature.preset.ocean()
m.add_compass(style="north arrow")

EOmaps - compass

raphael
  • 1,510
  • 10
  • 16