Skip to content

drawstyle with "step" has display error #16635

Closed

Description

@mankoff

Bug report

Bug summary

When plotting with drawstyle='steps' (or steps-pre, steps-mid, or steps-post) there is a graphic display error. This was submitted to Pandas (pandas-dev/pandas#31965) but I realize it may be a Matplotlib issue.

Specifically, on the graphic below and when using these two vectors:

x = [0,1,2]
y = [0,1,2]
  • steps-pre and steps imply that the y values is 1 in x=[0,1]. I would argue that the proper way to interpret this data is that y = 0 at x = 0, and presumably remains at 0 until the y value is updated to 1 at x=1. These two options essentially drop the first data point. There is some indication of an (x,y) = (0,0), but it is minimal.
  • steps-post has the same issue but at the end.
  • steps-mid graphically weights the values unevenly. The y=1 value has twice the weight of the other two line segments, based on length.
  • The fifth panel is graphically correct in my opinion, but requires a modification of the data before plotting it.

Code for reproduction

import matplotlib.pyplot as plt

x = [0,1,2]
y = [0,1,2]

fig = plt.figure(1, figsize=(3,7))

ax = fig.add_subplot(611)
ax.plot(x,y, drawstyle='steps-pre')
ax.set_ylabel('steps-pre')

ax = fig.add_subplot(612)
ax.plot(x,y, drawstyle='steps')
ax.set_ylabel('steps')

ax = fig.add_subplot(613)
ax.plot(x,y, drawstyle='steps-mid')
ax.set_ylabel('steps-mid')

ax = fig.add_subplot(614)
ax.plot(x,y, drawstyle='steps-post')
ax.set_ylabel('steps-post')

x.append(x[-1]+1)
y.append(y[-1])

ax = fig.add_subplot(615)
ax.plot(x,y, drawstyle='steps-post')
ax.set_ylabel('steps-post\nw/ extra data')

Actual outcome

steps-post

Expected outcome

See bottom panel.

The plot should display y[0] from x[0] to x[1], y[n] from x[n] to x[n+1], and therefore needs to have an x[n+1] appended to the end of the plot.

Activity

  1. ImportanceOfBeingErnest commented on Mar 2, 2020

    @ImportanceOfBeingErnest
    Member

    That's definitely not a display error. That is, the step demo shows what we would expect.

    I can see how adding an extra line-segment at the beginning or end might be useful in some applications, however, we do in general not know how long that line should be. Consider e.g. x = [1.1, 1.7, 2.1]; y = [0,1,2]; what would in that case the expected output look like?

  2. timhoffm commented on Mar 2, 2020

    @timhoffm
    Member

    @mankoff Thanks for the report. Unfortunately, steps is not designed to what you‘ve described in your expectation. As indicated, it‘s also not trivial, to generalize this to non-uniform distances.

    This has been discussed extensively in #15019, #15065 and the links therein. You are welcome to join the discussion in the latter. I‘m going to close this to keep this topic in one place.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions