diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 4537abe60b9f..dd86abaeba96 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -7541,11 +7541,6 @@ def spy(self, Z, precision=0, marker=None, markersize=None, pass 'present'. In this case any value present in the array will be plotted, even if it is identically zero. - origin : {'upper', 'lower'}, default: :rc:`image.origin` - Place the [0, 0] index of the array in the upper left or lower left - corner of the axes. The convention 'upper' is typically used for - matrices and images. - aspect : {'equal', 'auto', None} or float, default: 'equal' The aspect ratio of the axes. This parameter is particularly relevant for images since it determines whether data pixels are @@ -7560,6 +7555,11 @@ def spy(self, Z, precision=0, marker=None, markersize=None, non-square pixels. - *None*: Use :rc:`image.aspect`. + origin : {'upper', 'lower'}, default: :rc:`image.origin` + Place the [0, 0] index of the array in the upper left or lower left + corner of the axes. The convention 'upper' is typically used for + matrices and images. + Returns ------- ret : `~matplotlib.image.AxesImage` or `.Line2D` @@ -7585,6 +7585,7 @@ def spy(self, Z, precision=0, marker=None, markersize=None, """ if marker is None and markersize is None and hasattr(Z, 'tocoo'): marker = 's' + cbook._check_in_list(["upper", "lower"], origin=origin) if marker is None and markersize is None: Z = np.asarray(Z) mask = np.abs(Z) > precision @@ -7618,23 +7619,27 @@ def spy(self, Z, precision=0, marker=None, markersize=None, if 'linestyle' in kwargs: raise TypeError( "spy() got an unexpected keyword argument 'linestyle'") - marks = mlines.Line2D(x, y, linestyle='None', - marker=marker, markersize=markersize, **kwargs) - self.add_line(marks) + ret = mlines.Line2D( + x, y, linestyle='None', marker=marker, markersize=markersize, + **kwargs) + self.add_line(ret) nr, nc = Z.shape self.set_xlim(-0.5, nc - 0.5) - self.set_ylim(nr - 0.5, -0.5) + if origin == "upper": + self.set_ylim(nr - 0.5, -0.5) + else: + self.set_ylim(-0.5, nr - 0.5) self.set_aspect(aspect) - ret = marks self.title.set_y(1.05) - self.xaxis.tick_top() + if origin == "upper": + self.xaxis.tick_top() + else: + self.xaxis.tick_bottom() self.xaxis.set_ticks_position('both') - self.xaxis.set_major_locator(mticker.MaxNLocator(nbins=9, - steps=[1, 2, 5, 10], - integer=True)) - self.yaxis.set_major_locator(mticker.MaxNLocator(nbins=9, - steps=[1, 2, 5, 10], - integer=True)) + self.xaxis.set_major_locator( + mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True)) + self.yaxis.set_major_locator( + mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True)) return ret def matshow(self, Z, **kwargs): diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index e86688f8e356..60c304094e00 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -246,6 +246,7 @@ def __init__(self, ax, self._mouseover = True if origin is None: origin = rcParams['image.origin'] + cbook._check_in_list(["upper", "lower"], origin=origin) self.origin = origin self.set_filternorm(filternorm) self.set_filterrad(filterrad) diff --git a/lib/matplotlib/tests/baseline_images/test_axes/spy.png b/lib/matplotlib/tests/baseline_images/test_axes/spy.png deleted file mode 100644 index d2242b1858fa..000000000000 Binary files a/lib/matplotlib/tests/baseline_images/test_axes/spy.png and /dev/null differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a21c80ac1fd8..c1a8a57cfbc0 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -56,16 +56,26 @@ def test_acorr(): ax.legend() -@image_comparison(['spy.png'], style='mpl20') -def test_spy(): +@check_figures_equal(extensions=["png"]) +def test_spy(fig_test, fig_ref): np.random.seed(19680801) a = np.ones(32 * 32) a[:16 * 32] = 0 np.random.shuffle(a) - a = np.reshape(a, (32, 32)) - - fig, ax = plt.subplots() - ax.spy(a) + a = a.reshape((32, 32)) + + axs_test = fig_test.subplots(2) + axs_test[0].spy(a) + axs_test[1].spy(a, marker=".", origin="lower") + + axs_ref = fig_ref.subplots(2) + axs_ref[0].imshow(a, cmap="gray_r", interpolation="nearest") + axs_ref[0].xaxis.tick_top() + axs_ref[1].plot(*np.nonzero(a)[::-1], ".", markersize=10) + axs_ref[1].set( + aspect=1, xlim=axs_ref[0].get_xlim(), ylim=axs_ref[0].get_ylim()[::-1]) + for ax in axs_ref: + ax.xaxis.set_ticks_position("both") def test_spy_invalid_kwargs():