Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: plot1d for hist(cat,reg) #174

Merged
merged 5 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/hist/basehist.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,16 @@ def show(self, **kwargs: Any) -> Any:

return histoprint.print_hist(self, **kwargs)

def plot(self, *args: Any, **kwargs: Any) -> "Union[Hist1DArtists, Hist2DArtists]":
def plot(
self, *args: Any, overlay: "Optional[str]" = None, **kwargs: Any
) -> "Union[Hist1DArtists, Hist2DArtists]":
"""
Plot method for BaseHist object.
"""
if self.ndim == 1:
return self.plot1d(*args, **kwargs)
_has_categorical = np.sum([ax.traits.discrete for ax in self.axes]) == 1
_project = _has_categorical or overlay is not None
if self.ndim == 1 or (self.ndim == 2 and _project):
return self.plot1d(*args, overlay=overlay, **kwargs)
henryiii marked this conversation as resolved.
Show resolved Hide resolved
elif self.ndim == 2:
return self.plot2d(*args, **kwargs)
else:
Expand All @@ -312,6 +316,7 @@ def plot1d(
self,
*,
ax: "Optional[matplotlib.axes.Axes]" = None,
overlay: "Optional[Union[str, int]]" = None,
**kwargs: Any,
) -> "Hist1DArtists":
"""
Expand All @@ -320,7 +325,15 @@ def plot1d(

import hist.plot

return hist.plot.histplot(self, ax=ax, **_proc_kw_for_lw(kwargs))
if self.ndim == 1:
return hist.plot.histplot(self, ax=ax, **_proc_kw_for_lw(kwargs))
if overlay is None:
(overlay,) = (i for i, ax in enumerate(self.axes) if ax.traits.discrete)
assert overlay is not None
cat_ax = self.axes[overlay]
cats = cat_ax if cat_ax.traits.discrete else np.arange(len(cat_ax.centers))
d1hists = [self[{overlay: cat}] for cat in cats]
return hist.plot.histplot(d1hists, ax=ax, label=cats, **_proc_kw_for_lw(kwargs))

def plot2d(
self,
Expand Down
Binary file added tests/baseline/test_plot1d_auto_handling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,42 @@ def pdf(x, a=1 / np.sqrt(2 * np.pi), x0=0, sigma=1, offset=0):
assert h.plot_pull(pdf, fit_fmt=r"{name} = {value:.3g} $\pm$ {error:.3g}")

return fig


@pytest.mark.mpl_image_compare(baseline_dir="baseline", savefig_kwargs={"dpi": 50})
def test_plot1d_auto_handling():
"""
Test plot() by comparing against a reference image generated via
`pytest --mpl-generate-path=tests/baseline`
"""

np.random.seed(42)

h = Hist(
axis.Regular(10, 0, 10, name="variable", label="variable"),
axis.StrCategory("", name="dataset", growth=True),
andrzejnovak marked this conversation as resolved.
Show resolved Hide resolved
)

h_nameless = Hist(
axis.Regular(10, 0, 10),
axis.StrCategory("", growth=True),
)

h.fill(dataset="A", variable=np.random.normal(3, 2, 100))
h.fill(dataset="B", variable=np.random.normal(5, 2, 100))
h.fill(dataset="C", variable=np.random.normal(7, 2, 100))

h_nameless.fill(np.random.normal(3, 2, 1000), "A")
h_nameless.fill(np.random.normal(5, 2, 1000), "B")
h_nameless.fill(np.random.normal(7, 2, 1000), "C")

fig, (ax1, ax2) = plt.subplots(2, 2, figsize=(14, 10))

assert h.plot(ax=ax1[0])
assert h_nameless.plot(ax=ax2[0])

# Discrete axis plotting not yet implemented
# assert h.plot(ax=ax1[1], overlay='variable')
# assert h.plot(ax=ax2[1], overlay=1)

return fig