Skip to content

Commit

Permalink
Residual hist plot for comparer with many models
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-kipawa committed Oct 24, 2024
1 parent abac511 commit b03265f
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions modelskill/comparison/_comparer_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ def taylor(

def residual_hist(
self, bins=100, title=None, color=None, figsize=None, ax=None, **kwargs
) -> matplotlib.axes.Axes:
) -> matplotlib.axes.Axes | list[matplotlib.axes.Axes]:
"""plot histogram of residual values
Parameters
Expand All @@ -776,20 +776,67 @@ def residual_hist(
residual color, by default "#8B8D8E"
figsize : tuple, optional
figure size, by default None
ax : matplotlib.axes.Axes, optional
ax : matplotlib.axes.Axes | list[matplotlib.axes.Axes], optional
axes to plot on, by default None
**kwargs
other keyword arguments to plt.hist()
Returns
-------
matplotlib.axes.Axes
matplotlib.axes.Axes | list[matplotlib.axes.Axes]
"""
cmp = self.comparer

if cmp.n_models == 1:
return self._residual_hist_one_model(
bins=bins,
title=title,
color=color,
figsize=figsize,
ax=ax,
mod_name=cmp.mod_names[0],
**kwargs,
)

if ax is not None and len(ax) != len(cmp.mod_names):
raise ValueError("Number of axes must match number of models")

axs = ax if ax is not None else [None] * len(cmp.mod_names)

for i, mod_name in enumerate(cmp.mod_names):
cmp_model = cmp.sel(model=mod_name)
ax_mod = cmp_model.plot.residual_hist(
bins=bins,
title=title,
color=color,
figsize=figsize,
ax=axs[i],
**kwargs,
)
axs[i] = ax_mod

return axs

def _residual_hist_one_model(
self,
bins=100,
title=None,
color=None,
figsize=None,
ax=None,
mod_name=None,
**kwargs,
) -> matplotlib.axes.Axes:
"""Residual histogram for one model only"""
_, ax = _get_fig_ax(ax, figsize)

default_color = "#8B8D8E"
color = default_color if color is None else color
title = f"Residuals, {self.comparer.name}" if title is None else title
title = (
f"Residuals, Observation: {self.comparer.name}, Model: {mod_name}"
if title is None
else title
)
ax.hist(self.comparer._residual, bins=bins, color=color, **kwargs)
ax.set_title(title)
ax.set_xlabel(f"Residuals of {self.comparer._unit_text}")
Expand Down

0 comments on commit b03265f

Please sign in to comment.