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)
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)
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With