Skip to content
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
20 changes: 17 additions & 3 deletions src/machinevisiontoolbox/ImageWholeFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,7 @@ def plot(
samescale: bool = False,
ax: Axes | None = None,
bar: bool | None = None,
xlabel: str | None = None,
**kwargs: Any,
) -> None:
"""
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions tests/test_image_whole_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,60 @@
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"):

Check warning on line 691 in tests/test_image_whole_features.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_image_whole_features.py#L691

Redefining built-in 'type'
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()
Loading