Skip to content

Commit

Permalink
Merge pull request #47 from Freeman-Trader/easy-annotation
Browse files Browse the repository at this point in the history
Add Easy Annotations
  • Loading branch information
ColCarroll authored Jan 14, 2025
2 parents 2d4574a + 2d46290 commit e5081fd
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions ridge_map/ridge_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(self, bbox=(-71.928864, 43.758201, -70.957947, 44.465151), font=Non
if font is None:
font = FontManager().prop
self.font = font
self.annotations = list()

@property
def lats(self):
Expand Down Expand Up @@ -161,6 +162,51 @@ def preprocess(
values[np.logical_or(is_water, is_lake)] = np.nan
values = vertical_ratio * values[-1::-1] # switch north and south
return values

def add_annotation(
self,
label="Mount Washington",
coordinates=(-71.3173, 44.2946),
x_offset=-0.25,
y_offset=-0.045,
label_size=30,
annotation_size=8,
color=None,
background=True
):
"""Save an annotation.
Must call before plot_map()
Parameters
----------
label : string
Label to place on the map. Use an empty string for no label.
coordinates : (float, float)
The coordinates of the annotation as Longitude, Latitude
x_offset : float
Where to position the label relative to the annotation horizontally
y_offset : float
Where to position the label relative to the annotation vertically
label_size : int
fontsize of the label
annotation_size : int
Size of the annotation marking
label_color : string or None
Color for the label. If None, then uses label_color of map
background : bool
If there is a background or not
"""
self.annotations.append((
label,
coordinates,
x_offset,
y_offset,
label_size,
annotation_size,
color,
background
))

# pylint: disable=too-many-arguments,too-many-locals
def plot_map(
Expand Down Expand Up @@ -219,6 +265,39 @@ def plot_map(
-------
matplotlib.Axes
"""
def plot_annotations():
"""Plot the annotations.
Takes all the annotations from self.annotations and adds them to the map
"""
for annotation in self.annotations:
rel_coordinates = ((annotation[1][0] - self.longs[0])/(self.longs[1] - self.longs[0]),(annotation[1][1] - self.lats[0])/(self.lats[1] - self.lats[0]))
annotation_color = ax.texts[0].get_color()
if annotation[6] is not None:
annotation_color = annotation[6]

ax.text(
rel_coordinates[0] + annotation[2],
rel_coordinates[1] + annotation[3],
annotation[0],
fontproperties=self.font,
size=annotation[4],
color=annotation_color,
transform=ax.transAxes,
bbox={"facecolor": background_color, "alpha": 1, "linewidth": 1} if annotation[7] else None,
verticalalignment="bottom",
zorder=len(values) + 10
)

ax.plot(
*rel_coordinates,
'o',
color=annotation_color,
transform=ax.transAxes,
ms=annotation[5],
zorder=len(values) + 10
)

if kind not in {"gradient", "elevation"}:
raise TypeError("Argument `kind` must be one of 'gradient' or 'elevation'")
if values is None:
Expand Down Expand Up @@ -275,4 +354,7 @@ def plot_map(
for spine in ax.spines.values():
spine.set_visible(False)
ax.set_facecolor(background_color)

plot_annotations()

return ax

0 comments on commit e5081fd

Please sign in to comment.