Skip to content

Commit 01444b1

Browse files
authored
Stop spy from handing a colormap to a Line2D (#814)
1 parent a7a69de commit 01444b1

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

ultraplot/axes/plot.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7700,11 +7700,18 @@ def spy(self, z, **kwargs):
77007700
"""
77017701
kw = kwargs.copy()
77027702
kw.update(_pop_props(kw, "line")) # takes valid Line2D properties
7703-
default_cmap = pcolors.DiscreteColormap(["w", "k"], "_no_name")
7704-
center_levels = kw.pop("center_levels", None)
7705-
kw = self._parse_cmap(
7706-
z, center_levels=center_levels, default_cmap=default_cmap, **kw
7707-
)
7703+
# NOTE: Matplotlib's spy draws an image when no marker is given, and a
7704+
# Line2D of markers otherwise. Only the image understands a colormap, so
7705+
# parsing one for the marker path would hand 'cmap' to a Line2D.
7706+
markers = kw.get("marker", None) is not None or kw.get("markersize") is not None
7707+
if not markers:
7708+
default_cmap = pcolors.DiscreteColormap(["w", "k"], "_no_name")
7709+
center_levels = kw.pop("center_levels", None)
7710+
kw = self._parse_cmap(
7711+
z, center_levels=center_levels, default_cmap=default_cmap, **kw
7712+
)
7713+
else:
7714+
kw.pop("center_levels", None)
77087715
guide_kw = _pop_params(kw, self._update_guide)
77097716
m = self._call_native("spy", z, **kw)
77107717
self._update_guide(m, queue_colorbar=False, **guide_kw)

ultraplot/tests/test_2dplots.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,38 @@ def test_tripcolor_warns_when_z_and_facecolors_given():
453453
)
454454

455455

456+
def test_spy_marker_path_takes_no_colormap():
457+
"""
458+
Marker-style spy must not be handed a colormap.
459+
460+
Matplotlib's spy draws an image when no marker is given and a Line2D of
461+
markers otherwise; only the image understands `cmap`, so parsing one for
462+
the marker path raised `Line2D.set() got an unexpected keyword argument`.
463+
"""
464+
from matplotlib.image import AxesImage
465+
from matplotlib.lines import Line2D
466+
467+
matrix = np.random.default_rng(51423).random((12, 12)) > 0.8
468+
_, axs = uplt.subplots(ncols=4)
469+
assert isinstance(axs[0].spy(matrix), AxesImage)
470+
assert isinstance(axs[1].spy(matrix, markersize=2), Line2D)
471+
assert isinstance(axs[2].spy(matrix, marker="s"), Line2D)
472+
assert isinstance(axs[3].spy(matrix, color="denim", markersize=3), Line2D)
473+
474+
475+
def test_spy_image_path_still_takes_a_colormap():
476+
"""
477+
The image path keeps its colormap handling, including the discrete default.
478+
"""
479+
from matplotlib.image import AxesImage
480+
481+
matrix = np.random.default_rng(51423).random((12, 12)) > 0.8
482+
_, ax = uplt.subplots()
483+
image = ax.spy(matrix, cmap="Greys")
484+
assert isinstance(image, AxesImage)
485+
assert "greys" in image.get_cmap().name.lower()
486+
487+
456488
def test_tricontour_explicit_colors_match_levels():
457489
"""
458490
Explicit triangular contour colors should map one-to-one with levels.

0 commit comments

Comments
 (0)