Skip to content

Commit

Permalink
Adds ability to override specific style kwargs while also using an au…
Browse files Browse the repository at this point in the history
…tomatic style
  • Loading branch information
JamesVarndell committed Jun 27, 2024
1 parent 4285415 commit b4e6f90
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/earthkit/plots/components/subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)
from earthkit.plots.schemas import schema
from earthkit.plots.sources import get_source, single
from earthkit.plots.styles import _STYLE_KWARGS, Contour, Style, auto
from earthkit.plots.styles import _OVERRIDE_KWARGS, _STYLE_KWARGS, Contour, Style, auto
from earthkit.plots.utils import iter_utils, string_utils

DEFAULT_FORMATS = ["%Y", "%b", "%-d", "%H:%M", "%H:%M", "%S.%f"]
Expand Down Expand Up @@ -323,13 +323,17 @@ def _extract_plottables(
style_kwargs = {
key: kwargs.pop(key) for key in _STYLE_KWARGS if key in kwargs
}
# These are kwargs which can be overridden without forcing a new Style
override_kwargs = {key: style_kwargs.pop(key) for key in _OVERRIDE_KWARGS}
if style_kwargs:
style_class = (
Style if not method_name.startswith("contour") else Contour
)
style = style_class(**{**style_kwargs, **{"units": units}})
else:
style = auto.guess_style(source, units=units or source.units)
style = auto.guess_style(
source, units=units or source.units, **override_kwargs
)
if (data is None and z is None) or (z is not None and not z):
z_values = None
else:
Expand Down
6 changes: 5 additions & 1 deletion src/earthkit/plots/styles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"Style",
"DEFAULT_STYLE",
"_STYLE_KWARGS",
"_OVERRIDE_KWARGS",
]


Expand Down Expand Up @@ -791,7 +792,7 @@ class Contour(Style):
def __init__(
self,
colors=None,
line_colors=None,
line_colors="#555",
labels=False,
label_kwargs=None,
interpolate=True,
Expand Down Expand Up @@ -826,6 +827,7 @@ def to_contour_kwargs(self, data):
The data to be plotted using this `Style`.
"""
levels = self.levels(data)

cmap, norm = styles.colors.cmap_and_norm(
self._line_colors,
levels,
Expand Down Expand Up @@ -1020,3 +1022,5 @@ def disjoint(self, layer, *args, **kwargs):
_STYLE_KWARGS = list(
set(inspect.getfullargspec(Style)[0] + inspect.getfullargspec(Contour)[0])
)

_OVERRIDE_KWARGS = ["labels"]
4 changes: 2 additions & 2 deletions src/earthkit/plots/styles/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from earthkit.plots.schemas import schema


def guess_style(data, units=None):
def guess_style(data, units=None, **kwargs):
"""
Guess the style to be applied to the data based on its metadata.
Expand Down Expand Up @@ -97,4 +97,4 @@ def guess_style(data, units=None):
# No style matching units found; return default
return styles.DEFAULT_STYLE

return styles.Style.from_dict(style)
return styles.Style.from_dict({**style, **kwargs})

0 comments on commit b4e6f90

Please sign in to comment.