Skip to content

Commit

Permalink
added keywords check in MyFigure
Browse files Browse the repository at this point in the history
  • Loading branch information
mpecchi committed Mar 29, 2024
1 parent 26090a3 commit 3953e9a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 1 addition & 3 deletions example/example_minimal_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

# Define the folder path where your data is located. Change this path to where you've stored your data files.
# folder_path = plib.Path(plib.Path(__file__).parent, "example\data")
folder_path = plib.Path(
r"path\to\data\folder\"
)
folder_path = plib.Path(r"path\to\data\folder\\")
# folder_path: plib.Path = plib.Path(
# r"C:\Users\mp933\OneDrive - Cornell University\Python\GCMS\NNDNDD"
# )
Expand Down
15 changes: 14 additions & 1 deletion src/gcms_data_analysis/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ def default_kwargs(self) -> Dict[str, Any]:
"legend": True,
"legend_loc": "best",
"legend_ncols": 1,
"legend_title": None,
"annotate_lttrs": False,
"annotate_lttrs_xy": None,
"grid": False,
Expand All @@ -344,6 +345,12 @@ def process_kwargs(self) -> None:
:raises ValueError: _description_
:raises ValueError: _description_
"""
valid_kwargs = set(self.default_kwargs().keys())

# Check for any invalid keyword arguments
for kwarg in self.kwargs:
if kwarg not in valid_kwargs:
raise ValueError(f"Invalid keyword argument: '{kwarg}'")
self.kwargs["rows"] = int(self.kwargs["rows"])
self.kwargs["cols"] = int(self.kwargs["cols"])
self.kwargs["width"] = float(self.kwargs["width"])
Expand Down Expand Up @@ -445,17 +452,19 @@ def save_figure(

def add_legend(self) -> None:
"""_summary_"""
for sprop in ["legend", "legend_loc", "legend_ncols"]:
for sprop in ["legend", "legend_loc", "legend_ncols", "legend_title"]:
self.broad_props[sprop] = self._broadcast_value_prop(
self.kwargs[sprop], sprop
)

if self.kwargs["twinx"] is False:

for i, ax in enumerate(self.axs):
if self.broad_props["legend"][i]:
ax.legend(
loc=self.broad_props["legend_loc"][i],
ncol=self.broad_props["legend_ncols"][i],
title=self.broad_props["legend_title"][i],
)
else:
for i, (ax, axt) in enumerate(zip(self.axs, self.axts)):
Expand All @@ -467,6 +476,7 @@ def add_legend(self) -> None:
lab_ax + lab_axt,
loc=self.broad_props["legend_loc"][i],
ncol=self.broad_props["legend_ncols"][i],
title=self.broad_props["legend_title"][i],
)

def annotate_letters(self) -> None:
Expand Down Expand Up @@ -665,6 +675,7 @@ def plot_ave_std(
legend_x_anchor: float = 1,
legend_y_anchor: float = 1.02,
legend_labelspacing: float = 0.5,
legend_title: str | None = None,
**kwargs,
) -> MyFigure:
"""
Expand Down Expand Up @@ -919,6 +930,7 @@ def plot_ave_std(
ncol=legend_columns,
bbox_to_anchor=(legend_x_anchor, legend_y_anchor),
labelspacing=legend_labelspacing,
title=legend_title,
)
else: # legend is inside of plot area
myfig.axs[0].legend(
Expand All @@ -927,6 +939,7 @@ def plot_ave_std(
loc=legend_location,
ncol=legend_columns,
labelspacing=legend_labelspacing,
title=legend_title,
)
# annotate ave+-std at the top of outliers bar (exceeding y_lim)
if annotate_outliers and (y_lim is not None): # and (not df_std.empty):
Expand Down

0 comments on commit 3953e9a

Please sign in to comment.