diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index 18a1d8186a8e..cfa6b4d86081 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -479,6 +479,8 @@ def contains(self, mouseevent): else: # If line, return the nearby segment(s) ind = segment_hits(mouseevent.x, mouseevent.y, xt, yt, pixels) + if self._drawstyle.startswith("steps"): + ind //= 2 ind += self.ind_offset @@ -680,7 +682,8 @@ def recache(self, always=False): else: interpolation_steps = 1 xy = STEP_LOOKUP_MAP[self._drawstyle](*self._xy.T) - self._path = Path(np.asarray(xy).T, None, interpolation_steps) + self._path = Path(np.asarray(xy).T, + _interpolation_steps=interpolation_steps) self._transformed_path = None self._invalidx = False self._invalidy = False @@ -693,8 +696,9 @@ def _transform_path(self, subslice=None): """ # Masked arrays are now handled by the Path class itself if subslice is not None: - _steps = self._path._interpolation_steps - _path = Path(self._xy[subslice, :], _interpolation_steps=_steps) + xy = STEP_LOOKUP_MAP[self._drawstyle](*self._xy[subslice, :].T) + _path = Path(np.asarray(xy).T, + _interpolation_steps=self._path._interpolation_steps) else: _path = self._path self._transformed_path = TransformedPath(_path, self.get_transform()) diff --git a/lib/matplotlib/tests/baseline_images/test_lines/drawstyle_variants.png b/lib/matplotlib/tests/baseline_images/test_lines/drawstyle_variants.png new file mode 100644 index 000000000000..8b6d97b7b407 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_lines/drawstyle_variants.png differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 478f58441009..1ecc31b40dc8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4460,12 +4460,6 @@ def test_ls_ds_conflict(): linestyle='steps-pre:', drawstyle='steps-post') -@cleanup -def test_ls_ds_conflict(): - assert_raises(ValueError, plt.plot, range(32), - linestyle='steps-pre:', drawstyle='steps-post') - - @image_comparison(baseline_images=['date_timezone_x'], extensions=['png']) def test_date_timezone_x(): # Tests issue 5575 diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index 142816cab2b5..0d9151dbd3bb 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -118,16 +118,18 @@ def test_valid_linestyles(): line.set_linestyle('aardvark') -@cleanup +@image_comparison(baseline_images=['drawstyle_variants'], remove_text=True, + extensions=["png"]) def test_drawstyle_variants(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) - for ds in ("default", "steps-mid", "steps-pre", "steps-post", - "steps", None): - ax.plot(range(10), drawstyle=ds) - - fig.canvas.draw() - assert True + fig, axs = plt.subplots(6) + dss = ["default", "steps-mid", "steps-pre", "steps-post", "steps", None] + # We want to check that drawstyles are properly handled even for very long + # lines (for which the subslice optimization is on); however, we need + # to zoom in so that the difference between the drawstyles is actually + # visible. + for ax, ds in zip(axs.flat, dss): + ax.plot(range(2000), drawstyle=ds) + ax.set(xlim=(0, 2), ylim=(0, 2)) @cleanup