Skip to content

How to make plt.step() draw between pre-defined bins? #17529

Closed

Description

@dstansby

Bug report

The docstring for plt.step() states that when where='post':

  • The y value is continued constantly to the right from every x position, i.e. the interval [x[i], x[i+1]) has the value y[i]."

So if I have a len(2) y array, I should expect the last element to be continued constantly from x[1] to x[2], which means x must be a len(3) array. This makes sense, because x here specifies the edges of the bins. Specifying e.g. y=[1, 2] and x=[0, 1, 4] doesn't work however:

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

data = [1, 2]
bin_edges = [0, 1, 4]

fig, ax = plt.subplots()
ax.step(data, bin_edges, where='post')

Actual outcome

  File "test.py", line 8, in <module>
    ax.step(data, bin_edges, where='post')
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 2110, in step
    return self.plot(x, y, *args, data=data, **kwargs)
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 1646, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 216, in __call__
    yield from self._plot_args(this, kwargs)
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 342, in _plot_args
    raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (2,) and (3,)

Expected outcome

The bins should be drawn between [0, 1] and [1, 4].

Matplotlib version

  • Operating system: macos 10.15.4
  • Matplotlib version: 3.2.1, installed from pip
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.8.2

Activity

  1. timhoffm commented on May 28, 2020

    @timhoffm
    Member

    The issue is that step(..., where=...) is only syntactic sugar for the drawstyle of a Line2D. As such, it has N points (x, y) and the drawstyle decides how these are connected. This does not match the notion of bin edges and values for the spaces in between.

    For an illustration see https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/step_demo.html

    #15065 contains a proposal for what you are looking for.

    As a workaround you can fake an additional data value: data.append(data[-1]).

    IMHO the current step is likely neither very intuitive nor the most useful, but it's what it is for now. If you have suggestions how to make the docs more clear, PR is welcome.

  2. dstansby commented on May 28, 2020

    @dstansby
    MemberAuthor

    Will have a think about the docs; I think a clear example showing how to properly plot the output of np.hist() would be good too, linked from the step docstring.

  3. timhoffm commented on May 28, 2020

    @timhoffm
    Member

    how to properly plot the output of np.hist() would be good too

    Unfortunately, that's not straight forward. It requires above workaround or #15065. Additionally, you'd likely want vertical lines reaching down to 0 at the outer edges. This is clearly not something you can clearly express Line2D points plus a drawstyle. The discussion in #15065 floats the Idea of a HistLine2D artist, but is not conclusive yet. While it's simple to add the vertical lines to the workaround by adding further points, that's quite makeshift and I'd be hesitant to recommend it in an example.

  4. jklymak commented on May 29, 2020

    @jklymak
    Member

    @dstansby probably should read #15065. Your input there would be very helpful as a potential user of this. I strongly agree that some sort of way to easily draw the output from np.hist would be great.

  5. dstansby commented on Apr 12, 2021

    @dstansby
    MemberAuthor

    I think this is fixed by ax.stairs(), thanks to anyone that helped with that new feature!

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