8000 Fix spy(..., marker=<not-None>, origin="lower") by anntzer · Pull Request #16265 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix spy(..., marker=<not-None>, origin="lower") #16265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2020
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
39 changes: 22 additions & 17 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just makes the doc order match the signature order.

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`
Expand All @@ -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
Expand Down Expand Up @@ -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":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an additional change in behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was explicitly mentioned in the report ("Though it's worth pointing out that the x-axis ticks and labels always end up in upper spine, not the lower one, regardless of origin='upper' or origin='lower'. The behavior of imshow is a bit different. If origin='lower', the ticks and labels are moved to the bottom.") and clearly looks like an error as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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):
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file not shown.
22 changes: 16 additions & 6 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
0