Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96152ad

Browse files
authored
Fix stepfilled histogram polygon bottom perimeter (#15783)
Fix stepfilled histogram polygon bottom perimeter
2 parents 6fda79d + f40b0f8 commit 96152ad

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6776,13 +6776,18 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
67766776
xvals, yvals = [], []
67776777
for m in tops:
67786778
if stacked:
6779-
# starting point for drawing polygon
6780-
y[0] = y[1]
67816779
# top of the previous polygon becomes the bottom
67826780
y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1]
67836781
# set the top of this polygon
67846782
y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = (m + bottom,
67856783
m + bottom)
6784+
6785+
# The starting point of the polygon has not yet been
6786+
# updated. So far only the endpoint was adjusted. This
6787+
# assignment closes the polygon. The redundant endpoint is
6788+
# later discarded (for step and stepfilled).
6789+
y[0] = y[-1]
6790+
67866791
if orientation == 'horizontal':
67876792
xvals.append(y.copy())
67886793
yvals.append(x.copy())

lib/matplotlib/tests/test_axes.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3289,6 +3289,136 @@ def test_hist_step_bottom():
32893289
ax.hist(d1, bottom=np.arange(10), histtype="stepfilled")
32903290

32913291

3292+
def test_hist_stepfilled_geometry():
3293+
bins = [0, 1, 2, 3]
3294+
data = [0, 0, 1, 1, 1, 2]
3295+
_, _, (polygon, ) = plt.hist(data,
3296+
bins=bins,
3297+
histtype='stepfilled')
3298+
xy = [[0, 0], [0, 2], [1, 2], [1, 3], [2, 3], [2, 1], [3, 1],
3299+
[3, 0], [2, 0], [2, 0], [1, 0], [1, 0], [0, 0]]
3300+
assert_array_equal(polygon.get_xy(), xy)
3301+
3302+
3303+
def test_hist_step_geometry():
3304+
bins = [0, 1, 2, 3]
3305+
data = [0, 0, 1, 1, 1, 2]
3306+
_, _, (polygon, ) = plt.hist(data,
3307+
bins=bins,
3308+
histtype='step')
3309+
xy = [[0, 0], [0, 2], [1, 2], [1, 3], [2, 3], [2, 1], [3, 1], [3, 0]]
3310+
assert_array_equal(polygon.get_xy(), xy)
3311+
3312+
3313+
def test_hist_stepfilled_bottom_geometry():
3314+
bins = [0, 1, 2, 3]
3315+
data = [0, 0, 1, 1, 1, 2]
3316+
_, _, (polygon, ) = plt.hist(data,
3317+
bins=bins,
3318+
bottom=[1, 2, 1.5],
3319+
histtype='stepfilled')
3320+
xy = [[0, 1], [0, 3], [1, 3], [1, 5], [2, 5], [2, 2.5], [3, 2.5],
3321+
[3, 1.5], [2, 1.5], [2, 2], [1, 2], [1, 1], [0, 1]]
3322+
assert_array_equal(polygon.get_xy(), xy)
3323+
3324+
3325+
def test_hist_step_bottom_geometry():
3326+
bins = [0, 1, 2, 3]
3327+
data = [0, 0, 1, 1, 1, 2]
3328+
_, _, (polygon, ) = plt.hist(data,
3329+
bins=bins,
3330+
bottom=[1, 2, 1.5],
3331+
histtype='step')
3332+
xy = [[0, 1], [0, 3], [1, 3], [1, 5], [2, 5], [2, 2.5], [3, 2.5], [3, 1.5]]
3333+
assert_array_equal(polygon.get_xy(), xy)
3334+
3335+
3336+
def test_hist_stacked_stepfilled_geometry():
3337+
bins = [0, 1, 2, 3]
3338+
data_1 = [0, 0, 1, 1, 1, 2]
3339+
data_2 = [0, 1, 2]
3340+
_, _, patches = plt.hist([data_1, data_2],
3341+
bins=bins,
3342+
stacked=True,
3343+
histtype='stepfilled')
3344+
3345+
assert len(patches) == 2
3346+
3347+
polygon, = patches[0]
3348+
xy = [[0, 0], [0, 2], [1, 2], [1, 3], [2, 3], [2, 1], [3, 1],
3349+
[3, 0], [2, 0], [2, 0], [1, 0], [1, 0], [0, 0]]
3350+
assert_array_equal(polygon.get_xy(), xy)
3351+
3352+
polygon, = patches[1]
3353+
xy = [[0, 2], [0, 3], [1, 3], [1, 4], [2, 4], [2, 2], [3, 2],
3354+
[3, 1], [2, 1], [2, 3], [1, 3], [1, 2], [0, 2]]
3355+
assert_array_equal(polygon.get_xy(), xy)
3356+
3357+
3358+
def test_hist_stacked_step_geometry():
3359+
bins = [0, 1, 2, 3]
3360+
data_1 = [0, 0, 1, 1, 1, 2]
3361+
data_2 = [0, 1, 2]
3362+
_, _, patches = plt.hist([data_1, data_2],
3363+
bins=bins,
3364+
stacked=True,
3365+
histtype='step')
3366+
3367+
assert len(patches) == 2
3368+
3369+
polygon, = patches[0]
3370+
xy = [[0, 0], [0, 2], [1, 2], [1, 3], [2, 3], [2, 1], [3, 1], [3, 0]]
3371+
assert_array_equal(polygon.get_xy(), xy)
3372+
3373+
polygon, = patches[1]
3374+
xy = [[0, 2], [0, 3], [1, 3], [1, 4], [2, 4], [2, 2], [3, 2], [3, 1]]
3375+
assert_array_equal(polygon.get_xy(), xy)
3376+
3377+
3378+
def test_hist_stacked_stepfilled_bottom_geometry():
3379+
bins = [0, 1, 2, 3]
3380+
data_1 = [0, 0, 1, 1, 1, 2]
3381+
data_2 = [0, 1, 2]
3382+
_, _, patches = plt.hist([data_1, data_2],
3383+
bins=bins,
3384+
stacked=True,
3385+
bottom=[1, 2, 1.5],
3386+
histtype='stepfilled')
3387+
3388+
assert len(patches) == 2
3389+
3390+
polygon, = patches[0]
3391+
xy = [[0, 1], [0, 3], [1, 3], [1, 5], [2, 5], [2, 2.5], [3, 2.5],
3392+
[3, 1.5], [2, 1.5], [2, 2], [1, 2], [1, 1], [0, 1]]
3393+
assert_array_equal(polygon.get_xy(), xy)
3394+
3395+
polygon, = patches[1]
3396+
xy = [[0, 3], [0, 4], [1, 4], [1, 6], [2, 6], [2, 3.5], [3, 3.5],
3397+
[3, 2.5], [2, 2.5], [2, 5], [1, 5], [1, 3], [0, 3]]
3398+
assert_array_equal(polygon.get_xy(), xy)
3399+
3400+
3401+
def test_hist_stacked_step_bottom_geometry():
3402+
bins = [0, 1, 2, 3]
3403+
data_1 = [0, 0, 1, 1, 1, 2]
3404+
data_2 = [0, 1, 2]
3405+
_, _, patches = plt.hist([data_1, data_2],
3406+
bins=bins,
3407+
stacked=True,
3408+
bottom=[1, 2, 1.5],
3409+
histtype='step')
3410+
3411+
assert len(patches) == 2
3412+
3413+
polygon, = patches[0]
3414+
xy = [[0, 1], [0, 3], [1, 3], [1, 5], [2, 5], [2, 2.5], [3, 2.5], [3, 1.5]]
3415+
assert_array_equal(polygon.get_xy(), xy)
3416+
3417+
polygon, = patches[1]
3418+
xy = [[0, 3], [0, 4], [1, 4], [1, 6], [2, 6], [2, 3.5], [3, 3.5], [3, 2.5]]
3419+
assert_array_equal(polygon.get_xy(), xy)
3420+
3421+
32923422
@image_comparison(['hist_stacked_bar'])
32933423
def test_hist_stacked_bar():
32943424
# make some data

0 commit comments

Comments
 (0)