diff --git a/doc/api/next_api_changes/2019-02-07-AH.rst b/doc/api/next_api_changes/2019-02-07-AH.rst new file mode 100644 index 000000000000..9681457e9a04 --- /dev/null +++ b/doc/api/next_api_changes/2019-02-07-AH.rst @@ -0,0 +1,6 @@ +Modify output of Axes.hist when input consists of multiple empty lists +`````````````````````````````````````````````````````````````````````` + +Input that consists of multiple empty lists will now return a list of histogram +values for each one of the lists. For example, an input of ``[[],[]]`` will +return 2 lists of histogram values. Previously, a single list was returned. diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index d471c3815f7c..942b5652003c 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -6625,10 +6625,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None, # basic input validation input_empty = np.size(x) == 0 # Massage 'x' for processing. - if input_empty: - x = [np.array([])] - else: - x = cbook._reshape_2D(x, 'x') + x = cbook._reshape_2D(x, 'x') nx = len(x) # number of datasets # Process unit information diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1a848692907e..cea0f2291185 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1665,6 +1665,20 @@ def test_hist_datetime_datasets(): ax.hist(data, stacked=False) +@pytest.mark.parametrize('data, expected_number_of_hists', + [([], 1), + ([[]], 1), + ([[], []], 2)]) +def test_hist_with_empty_input(data, expected_number_of_hists): + hists, _, _ = plt.hist(data) + hists = np.asarray(hists) + + if hists.ndim == 1: + assert 1 == expected_number_of_hists + else: + assert hists.shape[0] == expected_number_of_hists + + def contour_dat(): x = np.linspace(-3, 5, 150) y = np.linspace(-3, 5, 120)