Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a "North Arrow" on a geopandas map

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)

like image 951
Regis Avatar asked Jan 01 '23 15:01

Regis


2 Answers

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.

like image 199
steven Avatar answered Jan 15 '23 23:01

steven


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

like image 28
raphael Avatar answered Jan 15 '23 21:01

raphael