Skip to content

Fix stepfilled histogram polygon bottom perimeter #15783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6796,13 +6796,18 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
xvals, yvals = [], []
for m in tops:
if stacked:
# starting point for drawing polygon
y[0] = y[1]
# top of the previous polygon becomes the bottom
y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1]
# set the top of this polygon
y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = (m + bottom,
m + bottom)

# The starting point of the polygon has not yet been
# updated. So far only the endpoint was adjusted. This
# assignment closes the polygon. The redundant endpoint is
# later discarded (for step and stepfilled).
y[0] = y[-1]

if orientation == 'horizontal':
xvals.append(y.copy())
yvals.append(x.copy())
Expand Down
130 changes: 130 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3289,6 +3289,136 @@ def test_hist_step_bottom():
ax.hist(d1, bottom=np.arange(10), histtype="stepfilled")


def test_hist_stepfilled_geometry():
bins = [0, 1, 2, 3]
data = [0, 0, 1, 1, 1, 2]
_, _, (polygon, ) = plt.hist(data,
bins=bins,
histtype='stepfilled')
xy = [[0, 0], [0, 2], [1, 2], [1, 3], [2, 3], [2, 1], [3, 1],
[3, 0], [2, 0], [2, 0], [1, 0], [1, 0], [0, 0]]
assert_array_equal(polygon.get_xy(), xy)


def test_hist_step_geometry():
bins = [0, 1, 2, 3]
data = [0, 0, 1, 1, 1, 2]
_, _, (polygon, ) = plt.hist(data,
bins=bins,
histtype='step')
xy = [[0, 0], [0, 2], [1, 2], [1, 3], [2, 3], [2, 1], [3, 1], [3, 0]]
assert_array_equal(polygon.get_xy(), xy)


def test_hist_stepfilled_bottom_geometry():
bins = [0, 1, 2, 3]
data = [0, 0, 1, 1, 1, 2]
_, _, (polygon, ) = plt.hist(data,
bins=bins,
bottom=[1, 2, 1.5],
histtype='stepfilled')
xy = [[0, 1], [0, 3], [1, 3], [1, 5], [2, 5], [2, 2.5], [3, 2.5],
[3, 1.5], [2, 1.5], [2, 2], [1, 2], [1, 1], [0, 1]]
assert_array_equal(polygon.get_xy(), xy)


def test_hist_step_bottom_geometry():
bins = [0, 1, 2, 3]
data = [0, 0, 1, 1, 1, 2]
_, _, (polygon, ) = plt.hist(data,
bins=bins,
bottom=[1, 2, 1.5],
histtype='step')
xy = [[0, 1], [0, 3], [1, 3], [1, 5], [2, 5], [2, 2.5], [3, 2.5], [3, 1.5]]
assert_array_equal(polygon.get_xy(), xy)


def test_hist_stacked_stepfilled_geometry():
bins = [0, 1, 2, 3]
data_1 = [0, 0, 1, 1, 1, 2]
data_2 = [0, 1, 2]
_, _, patches = plt.hist([data_1, data_2],
bins=bins,
stacked=True,
histtype='stepfilled')

assert len(patches) == 2

polygon, = patches[0]
xy = [[0, 0], [0, 2], [1, 2], [1, 3], [2, 3], [2, 1], [3, 1],
[3, 0], [2, 0], [2, 0], [1, 0], [1, 0], [0, 0]]
assert_array_equal(polygon.get_xy(), xy)

polygon, = patches[1]
xy = [[0, 2], [0, 3], [1, 3], [1, 4], [2, 4], [2, 2], [3, 2],
[3, 1], [2, 1], [2, 3], [1, 3], [1, 2], [0, 2]]
assert_array_equal(polygon.get_xy(), xy)


def test_hist_stacked_step_geometry():
bins = [0, 1, 2, 3]
data_1 = [0, 0, 1, 1, 1, 2]
data_2 = [0, 1, 2]
_, _, patches = plt.hist([data_1, data_2],
bins=bins,
stacked=True,
histtype='step')

assert len(patches) == 2

polygon, = patches[0]
xy = [[0, 0], [0, 2], [1, 2], [1, 3], [2, 3], [2, 1], [3, 1], [3, 0]]
assert_array_equal(polygon.get_xy(), xy)

polygon, = patches[1]
xy = [[0, 2], [0, 3], [1, 3], [1, 4], [2, 4], [2, 2], [3, 2], [3, 1]]
assert_array_equal(polygon.get_xy(), xy)


def test_hist_stacked_stepfilled_bottom_geometry():
bins = [0, 1, 2, 3]
data_1 = [0, 0, 1, 1, 1, 2]
data_2 = [0, 1, 2]
_, _, patches = plt.hist([data_1, data_2],
bins=bins,
stacked=True,
bottom=[1, 2, 1.5],
histtype='stepfilled')

assert len(patches) == 2

polygon, = patches[0]
xy = [[0, 1], [0, 3], [1, 3], [1, 5], [2, 5], [2, 2.5], [3, 2.5],
[3, 1.5], [2, 1.5], [2, 2], [1, 2], [1, 1], [0, 1]]
assert_array_equal(polygon.get_xy(), xy)

polygon, = patches[1]
xy = [[0, 3], [0, 4], [1, 4], [1, 6], [2, 6], [2, 3.5], [3, 3.5],
[3, 2.5], [2, 2.5], [2, 5], [1, 5], [1, 3], [0, 3]]
assert_array_equal(polygon.get_xy(), xy)


def test_hist_stacked_step_bottom_geometry():
bins = [0, 1, 2, 3]
data_1 = [0, 0, 1, 1, 1, 2]
data_2 = [0, 1, 2]
_, _, patches = plt.hist([data_1, data_2],
bins=bins,
stacked=True,
bottom=[1, 2, 1.5],
histtype='step')

assert len(patches) == 2

polygon, = patches[0]
xy = [[0, 1], [0, 3], [1, 3], [1, 5], [2, 5], [2, 2.5], [3, 2.5], [3, 1.5]]
assert_array_equal(polygon.get_xy(), xy)

polygon, = patches[1]
xy = [[0, 3], [0, 4], [1, 4], [1, 6], [2, 6], [2, 3.5], [3, 3.5], [3, 2.5]]
assert_array_equal(polygon.get_xy(), xy)


@image_comparison(['hist_stacked_bar'])
def test_hist_stacked_bar():
# make some data
Expand Down