You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to make plt.step() draw between pre-defined bins? #17529
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:
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].
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.
#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.
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.
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.
@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.
Activity
timhoffm commentedon May 28, 2020
The issue is that
step(..., where=...)is only syntactic sugar for thedrawstyleof aLine2D. 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
stepis 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.dstansby commentedon May 28, 2020
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 thestepdocstring.timhoffm commentedon May 28, 2020
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
Line2Dpoints plus a drawstyle. The discussion in #15065 floats the Idea of aHistLine2Dartist, 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.jklymak commentedon May 29, 2020
@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.histwould be great.dstansby commentedon Apr 12, 2021
I think this is fixed by
ax.stairs(), thanks to anyone that helped with that new feature!