diff --git a/src/machinevisiontoolbox/ImageWholeFeatures.py b/src/machinevisiontoolbox/ImageWholeFeatures.py index 1848af74..b04c925d 100644 --- a/src/machinevisiontoolbox/ImageWholeFeatures.py +++ b/src/machinevisiontoolbox/ImageWholeFeatures.py @@ -1369,6 +1369,7 @@ def plot( samescale: bool = False, ax: Axes | None = None, bar: bool | None = None, + xlabel: str | None = None, **kwargs: Any, ) -> None: """ @@ -1400,6 +1401,9 @@ def plot( :type samescale: bool, optional :param ax: Matplotlib axes to plot on, defaults to None (new figure) :type ax: matplotlib.axes.Axes, optional + :param xlabel: label for the x axis, defaults to "pixel value", or "bin rank" + for a sorted histogram + :type xlabel: str, optional :param bar: deprecated alias for ``filled``, use ``filled=`` instead :param kwargs: additional keyword arguments passed to Matplotlib plotting functions, ``plt.stairs`` for ``solid=True`` and ``plt.plot`` for ``solid=False``, and ``plt.Polygon`` for ``style='overlay'`` :raises ValueError: invalid histogram type @@ -1411,6 +1415,9 @@ def plot( ``cdf``/``normalized`` (normalized cumulative, range 0 to 1). ``ncdf`` is accepted as a deprecated alias for ``cdf``. + .. versionchanged:: 2.5.0 + Added the ``xlabel`` option. + The ``style`` option selects the style for plotting multiple planes: - ``stack``: plot each plane in a separate subplot. The ``cursor`` option @@ -1537,10 +1544,12 @@ def plot( if self._sorted: xrange = (self.x[0], self.x[-1]) - xlabel = "bin rank" + default_xlabel = "bin rank" else: xrange = (self._xrange[0], self._xrange[1]) - xlabel = "pixel value" + default_xlabel = "pixel value" + if xlabel is None: + xlabel = default_xlabel colors: list[str] = [] if self.colordict is not None: @@ -1994,9 +2003,14 @@ def plot( # type: ignore[override] Plot the histogram, with the thresholds marked The arguments are those of :meth:`Histogram.plot`. The lower and upper - thresholds are drawn as vertical lines. + thresholds are drawn as vertical lines. The x axis is labelled "gradient + magnitude" unless ``xlabel`` is given. + + .. versionchanged:: 2.5.0 + The x axis is labelled "gradient magnitude", it used to say "pixel value". """ ax = kwargs.get("ax") + kwargs.setdefault("xlabel", "gradient magnitude") # block=None: draw but do not show, the figure is shown once, below, after # the thresholds are added (showing it twice puts two live views of the one # figure in a notebook, which move together) diff --git a/tests/test_image_whole_features.py b/tests/test_image_whole_features.py index 45e1a1eb..17788be3 100644 --- a/tests/test_image_whole_features.py +++ b/tests/test_image_whole_features.py @@ -682,6 +682,60 @@ def test_ax_is_used_and_shown_once(self): self.assertEqual(show.call_count, 1) self.assertEqual(len(plt.get_fignums()), 1) + def _xlabel(self, **kwargs): + with patch("matplotlib.pyplot.show"): + self.hist.plot(**kwargs) + return plt.gca().get_xlabel() + + def test_x_axis_is_gradient_magnitude_for_every_type(self): + for type in ("frequency", "pdf", "cdf"): + plt.close("all") + self.assertEqual(self._xlabel(type=type), "gradient magnitude", type) + + def test_x_axis_label_can_be_given(self): + self.assertEqual(self._xlabel(xlabel="gradient"), "gradient") + + def test_x_axis_label_with_an_axes_given(self): + _, ax = plt.subplots() + with patch("matplotlib.pyplot.show"): + self.hist.plot(ax=ax) + self.assertEqual(ax.get_xlabel(), "gradient magnitude") + + +class TestHistogramXLabel(unittest.TestCase): + def setUp(self): + self.im = Image.Read("eiffel-1.png", mono=True) + + def tearDown(self): + plt.close("all") + + def _xlabel(self, hist, **kwargs): + with patch("matplotlib.pyplot.show"): + hist.plot(**kwargs) + return plt.gcf().axes[-1].get_xlabel() + + def test_defaults(self): + self.assertEqual(self._xlabel(self.im.hist()), "pixel value") + plt.close("all") + self.assertEqual(self._xlabel(self.im.hist(sorted=True)), "bin rank") + + def test_label_can_be_given(self): + for type in ("frequency", "pdf", "cdf"): + plt.close("all") + self.assertEqual( + self._xlabel(self.im.hist(), type=type, xlabel="grey level"), + "grey level", + type, + ) + + def test_label_can_be_given_for_overlay_and_stack(self): + color = Image.Read("flowers1.png") + for style in ("stack", "overlay"): + plt.close("all") + self.assertEqual( + self._xlabel(color.hist(), style=style, xlabel="level"), "level", style + ) + if __name__ == "__main__": unittest.main()