Skip to content

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

Closed
@dstansby

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

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