8000 Fix spy(..., marker=<not-None>, origin="lower") · matplotlib/matplotlib@67e0bcd · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 67e0bcd

Browse files
committed
Fix spy(..., marker=<not-None>, origin="lower")
and validate the origin kwarg both for spy() and for imshow(). There are some other slightly questionable choices in spy() (e.g. always calling `xaxis.set_ticks_position("both")` but not doing the same for the yaxis), but let's leave this as is for now.
1 parent db8cdf2 commit 67e0bcd

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
@@ -7541,11 +7541,6 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
75417541
pass 'present'. In this case any value present in the array
75427542
will be plotted, even if it is identically zero.
75437543
7544-
origin : {'upper', 'lower'}, default: :rc:`image.origin`
7545-
Place the [0, 0] index of the array in the upper left or lower left
7546-
corner of the axes. The convention 'upper' is typically used for
7547-
matrices and images.
7548-
75497544
aspect : {'equal', 'auto', None} or float, default: 'equal'
75507545
The aspect ratio of the axes. This parameter is particularly
75517546
relevant for images since it determines whether data pixels are
@@ -7560,6 +7555,11 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
75607555
non-square pixels.
75617556
- *None*: Use :rc:`image.aspect`.
75627557
7558+
origin : {'upper', 'lower'}, default: :rc:`image.origin`
7559+
Place the [0, 0] index of the array in the upper left or lower left
7560+
corner of the axes. The convention 'upper' is typically used for
7561+
matrices and images.
7562+
75637563
Returns
75647564
-------
75657565
ret : `~matplotlib.image.AxesImage` or `.Line2D`
@@ -7585,6 +7585,7 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
75857585
"""
75867586
if marker is None and markersize is None and hasattr(Z, 'tocoo'):
75877587
marker = 's'
7588+
cbook._check_in_list(["upper", "lower"], origin=origin)
75887589
if marker is None and markersize is None:
75897590
Z = np.asarray(Z)
75907591
mask = np.abs(Z) > precision
@@ -7618,23 +7619,27 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
76187619
if 'linestyle' in kwargs:
76197620
raise TypeError(
76207621
"spy() got an unexpected keyword argument 'linestyle'")
7621-
marks = mlines.Line2D(x, y, linestyle='None',
7622-
marker=marker, markersize=markersize, **kwargs)
7623-
self.add_line(marks)
7622+
ret = mlines.Line2D(
7623+
x, y, linestyle='None', marker=marker, markersize=markersize,
7624+
**kwargs)
7625+
self.add_line(ret)
76247626
nr, nc = Z.shape
76257627
self.set_xlim(-0.5, nc - 0.5)
7626-
self.set_ylim(nr - 0.5, -0.5)
7628+
if origin == "upper":
7629+
self.set_ylim(nr - 0.5, -0.5)
7630+
else:
7631+
self.set_ylim(-0.5, nr - 0.5)
76277632
self.set_aspect(aspect)
7628-
ret = marks
76297633
self.title.set_y(1.05)
7630-
self.xaxis.tick_top()
7634+
if origin == "upper":
7635+
self.xaxis.tick_top()
7636+
else:
7637+
self.xaxis.tick_bottom()
76317638
self.xaxis.set_ticks_position('both')
7632-
self.xaxis.set_major_locator(mticker.MaxNLocator(nbins=9,
7633-
steps=[1, 2, 5, 10],
7634-
integer=True))
7635-
self.yaxis.set_major_locator(mticker.MaxNLocator(nbins=9,
7636-
steps=[1, 2, 5, 10],
7637-
integer=True))
7639+
self.xaxis.set_major_locator(
7640+
mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True))
7641+
self.yaxis.set_major_locator(
7642+
mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True))
76387643
return ret
76397644

76407645
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
@@ -56,16 +56,26 @@ def test_acorr():
5656
ax.legend()
5757

5858

59-
@image_comparison(['spy.png'], style='mpl20')
60-
def test_spy():
59+
@check_figures_equal(extensions=["png"])
60+
def test_spy(fig_test, fig_ref):
6161
np.random.seed(19680801)
6262
a = np.ones(32 * 32)
6363
a[:16 * 32] = 0
6464
np.random.shuffle(a)
65-
a = np.reshape(a, (32, 32))
66-
67-
fig, ax = plt.subplots()
68-
ax.spy(a)
65+
a = a.reshape((32, 32))
66+
67+
axs_test = fig_test.subplots(2)
68+
axs_test[0].spy(a)
69+
axs_test[1].spy(a, marker=".", origin="lower")
70+
71+
axs_ref = fig_ref.subplots(2)
72+
axs_ref[0].imshow(a, cmap="gray_r", interpolation="nearest")
73+
axs_ref[0].xaxis.tick_top()
74+
axs_ref[1].plot(*np.nonzero(a)[::-1], ".", markersize=10)
75+
axs_ref[1].set(
76+
aspect=1, xlim=axs_ref[0].get_xlim(), ylim=axs_ref[0].get_ylim()[::-1])
77+
for ax in axs_ref:
78+
ax.xaxis.set_ticks_position("both")
6979

7080

7181
def test_spy_invalid_kwargs():

0 commit comments

Comments
 (0)
0