diff --git a/src/machinevisiontoolbox/ImageWholeFeatures.py b/src/machinevisiontoolbox/ImageWholeFeatures.py index b04c925d..47de700b 100644 --- a/src/machinevisiontoolbox/ImageWholeFeatures.py +++ b/src/machinevisiontoolbox/ImageWholeFeatures.py @@ -1775,9 +1775,9 @@ def _compute_plot_series( ylabel2 = "probability density" elif type in ("cf", "cumulative"): y = self.cf - maxy = y[ - -1, 0 - ] # last row values are all the same, total number of pixels in plane + # the last row is the total number of pixels in each plane, y is 1D for a + # greyscale image and 2D, one column per plane, for a color image + maxy = np.max(y[-1]) ylabel1 = "cumulative frequency" ylabel2 = "cumulative frequency" elif type in ("cdf", "normalized"): diff --git a/tests/test_image_whole_features.py b/tests/test_image_whole_features.py index 17788be3..2c1d7455 100644 --- a/tests/test_image_whole_features.py +++ b/tests/test_image_whole_features.py @@ -737,5 +737,31 @@ def test_label_can_be_given_for_overlay_and_stack(self): ) +class TestCumulativeFrequencyPlot(unittest.TestCase): + def tearDown(self): + plt.close("all") + + def _plot(self, image, **kwargs): + with patch("matplotlib.pyplot.show"): + image.hist().plot(**kwargs) + return plt.gcf().axes + + def test_greyscale(self): + im = Image.Read("eiffel-1.png", mono=True) + for type in ("cf", "cumulative"): + plt.close("all") + (ax,) = self._plot(im, type=type) + # the axis runs up to the number of pixels, which is where it ends + self.assertEqual(ax.get_ylim()[1], im.npixels) + + def test_color_stack_and_overlay(self): + im = Image.Read("flowers1.png") + for style in ("stack", "overlay"): + plt.close("all") + axes = self._plot(im, type="cf", style=style) + for ax in axes: + self.assertEqual(ax.get_ylim()[1], im.npixels) + + if __name__ == "__main__": unittest.main()