8000 Merge pull request #16265 from anntzer/lowerspy · matplotlib/matplotlib@58c6698 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58c6698

Browse files
authored
Merge pull request #16265 from anntzer/lowerspy
Fix spy(..., marker=<not-None>, origin="lower")
2 parents d500923 + 67e0bcd commit 58c6698

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7580,11 +7580,6 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
75807580
pass 'present'. In this case any value present in the array
75817581
will be plotted, even if it is identically zero.
75827582
7583-
origin : {'upper', 'lower'}, default: :rc:`image.origin`
7584-
Place the [0, 0] index of the array in the upper left or lower left
7585-
corner of the axes. The convention 'upper' is typically used for
7586-
matrices and images.
7587-
75887583
aspect : {'equal', 'auto', None} or float, default: 'equal'
75897584
The aspect ratio of the axes. This parameter is particularly
75907585
relevant for images since it determines whether data pixels are
@@ -7599,6 +7594,11 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
75997594
non-square pixels.
76007595
- *None*: Use :rc:`image.aspect`.
76017596
7597+
origin : {'upper', 'lower'}, default: :rc:`image.origin`
7598+
Place the [0, 0] index of the array in the upper left or lower left
7599+
corner of the axes. The convention 'upper' is typically used for
7600+
matrices and images.
7601+
76027602
Returns
76037603
-------
76047604
ret : `~matplotlib.image.AxesImage` or `.Line2D`
@@ -7624,6 +7624,7 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
76247624
"""
76257625
if marker is None and markersize is None and hasattr(Z, 'tocoo'):
76267626
marker = 's'
7627+
cbook._check_in_list(["upper", "lower"], origin=origin)
76277628
if marker is None and markersize is None:
76287629
Z = np.asarray(Z)
76297630
mask = np.abs(Z) > precision
@@ -7657,23 +7658,27 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
76577658
if 'linestyle' in kwargs:
76587659
raise TypeError(
76597660
"spy() got an unexpected keyword argument 'linestyle'")
7660-
marks = mlines.Line2D(x, y, linestyle='None',
7661-
marker=marker, markersize=markersize, **kwargs)
7662-
self.add_line(marks)
7661+
ret = mlines.Line2D(
7662+
x, y, linestyle='None', marker=marker, markersize=markersize,
7663+
**kwargs)
7664+
self.add_line(ret)
76637665
nr, nc = Z.shape
76647666
self.set_xlim(-0.5, nc - 0.5)
7665-
self.set_ylim(nr - 0.5, -0.5)
7667+
if origin == "upper":
7668+
self.set_ylim(nr - 0.5, -0.5)
7669+
else:
7670+
self.set_ylim(-0.5, nr - 0.5)
76667671
self.set_aspect(aspect)
7667-
ret = marks
76687672
self.title.set_y(1.05)
7669-
self.xaxis.tick_top()
7673+
if origin == "upper":
7674+
self.xaxis.tick_top()
7675+
else:
7676+
self.xaxis.tick_bottom()
76707677
self.xaxis.set_ticks_position('both')
7671-
self.xaxis.set_major_locator(mticker.MaxNLocator(nbins=9,
7672-
steps=[1, 2, 5, 10],
7673-
integer=True))
7674-
self.yaxis.set_major_locator(mticker.MaxNLocator(nbins=9,
7675-
steps=[1, 2, 5, 10],
7676-
integer=True))
7678+
self.xaxis.set_major_locator(
7679+
mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True))
7680+
self.yaxis.set_major_locator(
7681+
mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True))
76777682
return ret
76787683

76797684
def matshow(self, Z, **kwargs):

lib/matplotlib/image.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def __init__(self, ax,
246246
self._mouseover = True
247247
if origin is None:
248248
origin = rcParams['image.origin']
249+
cbook._check_in_list(["upper", "lower"], origin=origin)
249250
self.origin = origin
250251
self.set_filternorm(filternorm)
251252
self.set_filterrad(filterrad)
Binary file not shown.

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,26 @@ def test_acorr():
109109
ax.legend()
110110

111111

112-
@image_comparison(['spy.png'], style='mpl20')
113-
def test_spy():
112+
@check_figures_equal(extensions=["png"])
113+
def test_spy(fig_test, fig_ref):
114114
np.random.seed(19680801)
115115
a = np.ones(32 * 32)
116116
a[:16 * 32] = 0
117117
np.random.shuffle(a)
118-
a = np.reshape(a, (32, 32))
119-
120-
fig, ax = plt.subplots()
121-
ax.spy(a)
118+
a = a.reshape((32, 32))
119+
120+
axs_test = fig_test.subplots(2)
121+
axs_test[0].spy(a)
122+
axs_test[1].spy(a, marker=".", origin="lower")
123+
124+
axs_ref = fig_ref.subplots(2)
125+
axs_ref[0].imshow(a, cmap="gray_r", interpolation="nearest")
126+
axs_ref[0].xaxis.tick_top()
127+
axs_ref[1].plot(*np.nonzero(a)[::-1], ".", markersize=10)
128+
axs_ref[1].set(
129+
aspect=1, xlim=axs_ref[0].get_xlim(), ylim=axs_ref[0].get_ylim()[::-1])
130+
for ax in axs_ref:
131+
ax.xaxis.set_ticks_position("both")
122132

123133

124134
def test_spy_invalid_kwargs():

0 commit comments

Comments
 (0)
0