Skip to content

Commit e5b5adc

Browse files
authored
Merge pull request #50 from Freeman-Trader/refactor-annotations
Refactor annotations
2 parents ba95b66 + 33647d5 commit e5b5adc

File tree

2 files changed

+103
-60
lines changed

2 files changed

+103
-60
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,50 @@ ridges.plot(*scipp_coords, 'o',
278278
zorder=len(values)+10)
279279
```
280280

281+
#### Updated Annotation and Custom Text Color
282+
The above code still works, but now there is a simplified method (Shown Below) that will produce the same image.
283+
284+
```python
285+
import matplotlib.pyplot as plt
286+
import numpy as np
287+
288+
from ridge_map import RidgeMap
289+
290+
bgcolor = np.array([65,74,76])/255.
291+
292+
rm = RidgeMap((-122.087116,36.945365,-121.999226,37.023250))
293+
values = rm.get_elevation_data(num_lines=150)
294+
values = rm.preprocess(
295+
values=values,
296+
lake_flatness=1,
297+
water_ntile=0,
298+
vertical_ratio=240
299+
)
300+
301+
rm.plot_map(
302+
values=values,
303+
label='Santa Cruz\nMountains',
304+
label_x=0.75,
305+
label_y=0.05,
306+
label_size=36,
307+
label_color='white',
308+
kind='elevation',
309+
linewidth=1,
310+
background_color=bgcolor,
311+
line_color = plt.get_cmap('cool')
312+
)
313+
314+
rm.plot_annotation(
315+
label='SCIPP',
316+
coordinates=(-122.060510, 36.998776),
317+
x_offset=0.005,
318+
y_offset=0.005,
319+
label_size=20,
320+
annotation_size=6,
321+
color='white',
322+
background=False
323+
)
324+
```
281325
![png](https://github.com/ColCarroll/ridge_map/blob/main/examples/santa_cruz.png?raw=true)
282326

283327
Elevation Data

ridge_map/ridge_map.py

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, bbox=(-71.928864, 43.758201, -70.957947, 44.465151), font=Non
7979
if font is None:
8080
font = FontManager().prop
8181
self.font = font
82-
self.annotations = []
82+
self.ax = None
8383

8484
@property
8585
def lats(self):
@@ -181,7 +181,7 @@ def preprocess(
181181
return values
182182

183183
# pylint: disable=too-many-arguments,too-many-positional-arguments
184-
def add_annotation(
184+
def plot_annotation(
185185
self,
186186
label="Mount Washington",
187187
coordinates=(-71.3173, 44.2946),
@@ -191,10 +191,11 @@ def add_annotation(
191191
annotation_size=8,
192192
color=None,
193193
background=True,
194+
ax=None,
194195
):
195-
"""Save an annotation.
196+
"""Plot an annotation to an existing map
196197
197-
Must call before plot_map()
198+
It is recommended to call this function only after calling map_plot()
198199
199200
Parameters
200201
----------
@@ -214,20 +215,60 @@ def add_annotation(
214215
Color for the label. If None, then uses label_color of map
215216
background : bool
216217
If there is a background or not
217-
"""
218-
self.annotations.append(
219-
(
220-
label,
221-
coordinates,
222-
x_offset,
223-
y_offset,
224-
label_size,
225-
annotation_size,
226-
color,
227-
background,
228-
)
218+
ax : matplotlib Axes
219+
You can pass your own axes, but probably best not to
220+
221+
Returns
222+
-------
223+
matplotlib.Axes
224+
"""
225+
if ax is None and self.ax is None:
226+
raise ValueError("No axes found: Either plot_map() beforehand or pass an matplotlib.Axes value through")
227+
elif ax is None:
228+
ax = self.ax
229+
230+
highest_zorder = max(text.zorder for text in ax.texts) if ax.texts else 1
231+
232+
rel_coordinates = (
233+
(coordinates[0] - self.longs[0]) / (self.longs[1] - self.longs[0]),
234+
(coordinates[1] - self.lats[0]) / (self.lats[1] - self.lats[0]),
229235
)
236+
237+
annotation_color = "black"
238+
if color:
239+
annotation_color = color
240+
elif ax.texts[0].get_color():
241+
annotation_color = ax.texts[0].get_color()
230242

243+
ax.text(
244+
rel_coordinates[0] + x_offset,
245+
rel_coordinates[1] + y_offset,
246+
label,
247+
fontproperties=self.font,
248+
size=label_size,
249+
color=annotation_color,
250+
transform=ax.transAxes,
251+
bbox=(
252+
{"facecolor": ax.get_facecolor(), "alpha": 1, "linewidth": 1}
253+
if background
254+
else None
255+
),
256+
verticalalignment="bottom",
257+
zorder=highest_zorder,
258+
)
259+
260+
ax.plot(
261+
*rel_coordinates,
262+
"o",
263+
color=annotation_color,
264+
transform=ax.transAxes,
265+
ms=annotation_size,
266+
zorder=highest_zorder,
267+
)
268+
269+
self.ax = ax
270+
return ax
271+
231272
# pylint: disable=too-many-arguments,too-many-locals
232273
def plot_map(
233274
self,
@@ -286,47 +327,6 @@ def plot_map(
286327
matplotlib.Axes
287328
"""
288329

289-
def plot_annotations():
290-
"""Plot the annotations.
291-
292-
Takes all the annotations from self.annotations and adds them to the map
293-
"""
294-
for annotation in self.annotations:
295-
rel_coordinates = (
296-
(annotation[1][0] - self.longs[0])
297-
/ (self.longs[1] - self.longs[0]),
298-
(annotation[1][1] - self.lats[0]) / (self.lats[1] - self.lats[0]),
299-
)
300-
annotation_color = ax.texts[0].get_color()
301-
if annotation[6] is not None:
302-
annotation_color = annotation[6]
303-
304-
ax.text(
305-
rel_coordinates[0] + annotation[2],
306-
rel_coordinates[1] + annotation[3],
307-
annotation[0],
308-
fontproperties=self.font,
309-
size=annotation[4],
310-
color=annotation_color,
311-
transform=ax.transAxes,
312-
bbox=(
313-
{"facecolor": background_color, "alpha": 1, "linewidth": 1}
314-
if annotation[7]
315-
else None
316-
),
317-
verticalalignment="bottom",
318-
zorder=len(values) + 10,
319-
)
320-
321-
ax.plot(
322-
*rel_coordinates,
323-
"o",
324-
color=annotation_color,
325-
transform=ax.transAxes,
326-
ms=annotation[5],
327-
zorder=len(values) + 10
328-
)
329-
330330
if kind not in {"gradient", "elevation"}:
331331
raise TypeError("Argument `kind` must be one of 'gradient' or 'elevation'")
332332
if values is None:
@@ -383,7 +383,6 @@ def plot_annotations():
383383
for spine in ax.spines.values():
384384
spine.set_visible(False)
385385
ax.set_facecolor(background_color)
386-
387-
plot_annotations()
388-
386+
387+
self.ax = ax
389388
return ax

0 commit comments

Comments
 (0)