From f34aacb7c461f8c765c1818979ca9ab05a04bf8a Mon Sep 17 00:00:00 2001 From: John Krasting Date: Wed, 13 Aug 2025 09:17:12 -0400 Subject: [PATCH] Added `plot_color` kwarg to CaseGroup2 init - This allows a user to define a color for this case group at initialization - This can be accessed later in the plotting routines - If a color is not defined, one will be assigned at the NotebookDiagnostic.resolve() method --- src/esnb/core/CaseGroup2.py | 8 +++++++- src/esnb/core/NotebookDiagnostic.py | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/esnb/core/CaseGroup2.py b/src/esnb/core/CaseGroup2.py index f4ef2ac..4e0fc0b 100644 --- a/src/esnb/core/CaseGroup2.py +++ b/src/esnb/core/CaseGroup2.py @@ -216,7 +216,7 @@ def filter_catalog(catalog, variable): class CaseGroup2: """ - CaseGroup2(source, name=None, description=None, concat_dim=None, date_range=None, **kwargs) + CaseGroup2(source, name=None, description=None, concat_dim=None, date_range=None, plot_color=None, **kwargs) A group of case objects with shared metadata and catalog management. @@ -235,6 +235,8 @@ class CaseGroup2: Description of the case group. date_range : tuple or list The date range used to filter cases. + plot_color : str + Color associated with the group that is used for plotting. concat_dim : str The dimension along which to concatenate cases. is_resolved : bool @@ -254,6 +256,7 @@ def __init__( self, source, concat_dim=None, + plot_color=None, name=None, date_range=None, description=None, @@ -296,6 +299,7 @@ def __init__( self.name = name self.description = description self.date_range = date_range + self.plot_color = plot_color self.concat_dim = concat_dim self.is_resolved = False self.is_loaded = False @@ -512,6 +516,8 @@ def color_logical(var): ) result += f"is_resolved{color_logical(self.is_resolved)}" result += f"is_loaded{color_logical(self.is_loaded)}" + _color = "black" if self.plot_color is None else self.plot_color + result += f"plot_color{self.plot_color}" result += f"cases{self.cases}" if hasattr(self, "datasets"): diff --git a/src/esnb/core/NotebookDiagnostic.py b/src/esnb/core/NotebookDiagnostic.py index 6d60770..fb1ff26 100644 --- a/src/esnb/core/NotebookDiagnostic.py +++ b/src/esnb/core/NotebookDiagnostic.py @@ -472,6 +472,9 @@ def resolve(self, groups=None): groups = [] if groups is None else groups groups = [groups] if not isinstance(groups, list) else groups + + groups = assign_plot_colors(groups) + self.groups = groups if hasattr(self.groups[0], "resolve_datasets"): # warnings.warn("Legacy CaseGroup object found. Make sure you are using the latest version of ESNB.", DeprecationWarning, stacklevel=2) @@ -571,6 +574,26 @@ def _repr_html_(self): return result +def assign_plot_colors(groups): + default_colors = [ + "royalblue", + "darkorange", + "forestgreen", + "firebrick", + "slateblue", + "saddlebrown", + "deeppink", + "dimgray", + "olive", + "darkcyan", + ] + for group in groups: + if group.plot_color is None: + group.plot_color = default_colors[0] + default_colors.pop(0) + return groups + + def write_cached_datasets( diag, workdir=None, output_format="zarr", overwrite=False, chunks=None ):