Skip to content

[Bug]: Enabling the line on a previously drawn step plot connects the markers diagonally #32695

Backlog

Description

@williamlus

Bug summary

Draw a post-step plot with markers only, then call set_linestyle("-") and redraw. The markers stay in place, but the stroke connects them diagonally even though get_drawstyle() still returns "steps-post".

Code for reproduction

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
line, = ax.step([0, 1, 2], [0, 1, 0], where="post", marker="o", linestyle="None")
fig.canvas.draw()  # Draw once with only the markers visible.

line.set_linestyle("-")
fig.canvas.draw()
print(line.get_drawstyle())
print(line.get_path().vertices.tolist())
fig.savefig("step-redraw.png")

Actual outcome

The PNG shows two diagonal segments forming a peak. The reported draw style and path are:

steps-post
[[0.0, 0.0], [1.0, 1.0], [2.0, 0.0]]
image

Expected outcome

Enabling the stroke should produce the same staircase as passing linestyle="-" to the original ax.step() call. The stroke should pass through (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 0), with markers at the three input samples.

image

Additional information

No response

Operating system

No response

Matplotlib Version

3.12.0.dev1103+gf8c028304

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Activity

  1. RaghavRD commented on Nov 30, 2026

    @RaghavRD

    I looked into this and I think I've found the root cause.

    In Line2D.draw(), markers are drawn ignoring the drawstyle by temporarily switching to "default" and calling recache():

    if self.get_drawstyle() != "default":
        with cbook._setattr_cm(
                self, _drawstyle="default", _transformed_path=None):
            self.recache()
            ...

    recache() overwrites self._path with the non-step path, but _setattr_cm only restores _drawstyle and _transformed_path, so self._path stays as the non-step path afterwards. If the line was already drawn in that call, the cached step _transformed_path gets restored and hides the problem. In this report the line was not drawn on the first draw (linestyle="None"), so the cache is empty and the next draw rebuilds it from the wrong _path.

    This also affects the normal case: after any draw, get_path() returns the non-step path even when linestyle="-" from the start:

    line, = ax.step([0, 1, 2], [0, 1, 0], where="post", marker="o")
    line.get_path().vertices.tolist()  # [[0,0],[1,0],[1,1],[2,1],[2,0]]
    fig.canvas.draw()
    line.get_path().vertices.tolist()  # [[0,0],[1,1],[2,0]]

    I can reproduce both on 3.11.2 as well, so this is not a recent regression.

    Adding _path=self._path to the _setattr_cm call fixes both cases locally. A cleaner alternative might be to compute the marker positions directly from self._xy without calling recache() at all.

    I'm happy to open a PR with a fix and a regression test. Let me know if either approach is preferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions