Skip to content

Commit

Permalink
ENH: Make scattertext() a public API
Browse files Browse the repository at this point in the history
This will make it easier to use for various use cases.
  • Loading branch information
dopplershift authored and dcamron committed Jan 9, 2025
1 parent 0ea137a commit 3762785
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 288 deletions.
4 changes: 2 additions & 2 deletions src/metpy/plots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# SPDX-License-Identifier: BSD-3-Clause
r"""Contains functionality for making meteorological plots."""

# Trigger matplotlib wrappers
from . import _mpl # noqa: F401
from . import cartopy_utils, plot_areas
from ._util import (add_metpy_logo, add_timestamp, add_unidata_logo, # noqa: F401
convert_gempak_color)
Expand All @@ -13,6 +11,7 @@
from .patheffects import * # noqa: F403
from .skewt import * # noqa: F403
from .station_plot import * # noqa: F403
from .text import * # noqa: F403
from .wx_symbols import * # noqa: F403
from ..package_tools import set_module

Expand All @@ -21,6 +20,7 @@
__all__.extend(patheffects.__all__) # pylint: disable=undefined-variable
__all__.extend(skewt.__all__) # pylint: disable=undefined-variable
__all__.extend(station_plot.__all__) # pylint: disable=undefined-variable
__all__.extend(text.__all__) # pylint: disable=undefined-variable
__all__.extend(wx_symbols.__all__) # pylint: disable=undefined-variable
__all__.extend(['add_metpy_logo', 'add_timestamp', 'add_unidata_logo',
'convert_gempak_color'])
Expand Down
261 changes: 0 additions & 261 deletions src/metpy/plots/_mpl.py

This file was deleted.

22 changes: 7 additions & 15 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
TraitError, Tuple, Unicode, Union, validate)

from . import ctables, wx_symbols
from ._mpl import TextCollection
from .cartopy_utils import import_cartopy
from .patheffects import ColdFront, OccludedFront, StationaryFront, WarmFront
from .station_plot import StationPlot
from .text import scattertext, TextCollection
from ..calc import reduce_point_density, smooth_n_point, zoom_xarray
from ..package_tools import Exporter
from ..units import units
Expand Down Expand Up @@ -2188,13 +2188,9 @@ def _draw_strengths(self, text, lon, lat, color, offset=None):
if offset is None:
offset = tuple(x * self.label_fontsize * 0.8 for x in self.strength_offset)

self.parent.ax.scattertext([lon], [lat], [str(text)],
color=color,
loc=offset,
weight='demi',
size=int(self.label_fontsize * 0.7),
transform=ccrs.PlateCarree(),
clip_on=True)
scattertext(self.parent.ax, [lon], [lat], [str(text)], color=color, loc=offset,
weight='demi', size=int(self.label_fontsize * 0.7),
transform=ccrs.PlateCarree(), clip_on=True)

def _draw_labels(self, text, lon, lat, color, offset=(0, 0)):
"""Draw labels in the plot.
Expand All @@ -2212,13 +2208,9 @@ def _draw_labels(self, text, lon, lat, color, offset=(0, 0)):
offset : tuple (default: (0, 0))
A tuple containing the x- and y-offset of the label, respectively
"""
self.parent.ax.scattertext([lon], [lat], [str(text)],
color=color,
loc=offset,
weight='demi',
size=self.label_fontsize,
transform=ccrs.PlateCarree(),
clip_on=True)
scattertext(self.parent.ax, [lon], [lat], [str(text)], color=color, loc=offset,
weight='demi', size=self.label_fontsize, transform=ccrs.PlateCarree(),
clip_on=True)

def draw(self):
"""Draw the plot."""
Expand Down
10 changes: 5 additions & 5 deletions src/metpy/plots/station_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np

from .text import scattertext
from .wx_symbols import (current_weather, high_clouds, low_clouds, mid_clouds,
pressure_tendency, sky_cover, wx_symbol_font)
from ..package_tools import Exporter
Expand Down Expand Up @@ -209,7 +210,7 @@ def plot_parameter(self, location, parameter, formatter=None, **kwargs):
def plot_text(self, location, text, **kwargs):
"""At the specified location in the station model plot a collection of text.
This specifies that at the offset `location`, the strings in `text` should be
This specifies that at the offset ``location``, the strings in ``text`` should be
plotted.
Additional keyword arguments given will be passed onto the actual plotting
Expand All @@ -220,7 +221,7 @@ def plot_text(self, location, text, **kwargs):
Parameters
----------
location : str or tuple[float, float]
The offset (relative to center) to plot this parameter. If str, should be one of
The offset (relative to center) to plot this parameter. If `str`, should be one of
'C', 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', or 'NW'. Otherwise, should be a tuple
specifying the number of increments in the x and y directions; increments
are multiplied by `spacing` to give offsets in x and y relative to the center.
Expand All @@ -237,9 +238,8 @@ def plot_text(self, location, text, **kwargs):
location = self._handle_location(location)

kwargs = self._make_kwargs(kwargs)
text_collection = self.ax.scattertext(self.x, self.y, text, loc=location,
size=kwargs.pop('fontsize', self.fontsize),
**kwargs)
text_collection = scattertext(self.ax, self.x, self.y, text, loc=location,
size=kwargs.pop('fontsize', self.fontsize), **kwargs)
if location in self.items:
self.items[location].remove()
self.items[location] = text_collection
Expand Down
Loading

0 comments on commit 3762785

Please sign in to comment.