@@ -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