Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 161eb17

Browse filesBrowse files
author
Umair Idris
committed
Fix hist to return array of zeroes and bins with single zero size patch on empty input
1 parent 6814900 commit 161eb17
Copy full SHA for 161eb17

File tree

Expand file treeCollapse file tree

2 files changed

+20
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+20
-4
lines changed

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5394,7 +5394,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
53945394
Compute and draw the histogram of *x*. The return value is a
53955395
tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*,
53965396
[*patches0*, *patches1*,...]) if the input contains multiple
5397-
data or ([], [], []) if input is an empty array.
5397+
data or ([0, 0, ..., 0], *bins*, [zero height & width patch])
5398+
if the input is an empty array.
53985399
53995400
Multiple data can be provided via *x* as a list of datasets
54005401
of potentially different length ([*x0*, *x1*, ...]), or as
@@ -5605,8 +5606,11 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
56055606

56065607
# basic input validation
56075608
flat = np.ravel(x)
5608-
if len(flat) == 0:
5609-
return [], [], cbook.silent_list('No Patches')
5609+
5610+
if not len(flat):
5611+
n, bins = np.histogram([], bins)
5612+
patch = self.bar([0], [0], width=0)
5613+
return n, bins, cbook.silent_list('Zero Size Patch', [patch])
56105614

56115615
# Massage 'x' for processing.
56125616
# NOTE: Be sure any changes here is also done below to 'weights'

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,19 @@ def test_hist_log():
10101010
def test_hist_empty():
10111011
# From #3886: creating hist from empty dataset raises ValueError
10121012
ax = plt.gca()
1013-
ax.hist([])
1013+
n, bins, patches = ax.hist([])
1014+
1015+
# hist on empty should match empty numpy histogram
1016+
n1, bins1 = np.histogram([])
1017+
1018+
assert_array_equal(n, n1)
1019+
assert_array_equal(bins, bins1)
1020+
1021+
# check single zero size patch
1022+
assert_true((len(patches) == 1) and len(patches[0].patches) == 1)
1023+
rect_patch = patches[0].patches[0]
1024+
assert_false(rect_patch._height)
1025+
assert_false(rect_patch._width)
10141026

10151027
@image_comparison(baseline_images=['hist_steplog'], remove_text=True)
10161028
def test_hist_steplog():

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.