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

Skip to content

Commit 0a2324e

Browse files
authored
Merge pull request matplotlib#8993 from dstansby/hist-kwarg-fix
Add 'density' kwarg to histogram
2 parents c364852 + 9535c3b commit 0a2324e

File tree

2 files changed

+87
-52
lines changed

2 files changed

+87
-52
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def _plot_args_replacer(args, data):
9191
# The axes module contains all the wrappers to plotting functions.
9292
# All the other methods should go in the _AxesBase class.
9393

94-
9594
class Axes(_AxesBase):
9695
"""
9796
The :class:`Axes` contains most of the figure elements:
@@ -5870,10 +5869,10 @@ def table(self, **kwargs):
58705869
#### Data analysis
58715870

58725871
@_preprocess_data(replace_names=["x", 'weights'], label_namer="x")
5873-
def hist(self, x, bins=None, range=None, normed=False, weights=None,
5872+
def hist(self, x, bins=None, range=None, density=None, weights=None,
58745873
cumulative=False, bottom=None, histtype='bar', align='mid',
58755874
orientation='vertical', rwidth=None, log=False,
5876-
color=None, label=None, stacked=False,
5875+
color=None, label=None, stacked=False, normed=None,
58775876
**kwargs):
58785877
"""
58795878
Plot a histogram.
@@ -5897,55 +5896,60 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
58975896
arrays which are not required to be of the same length
58985897
58995898
bins : integer or array_like or 'auto', optional
5900-
If an integer is given, `bins + 1` bin edges are returned,
5899+
If an integer is given, ``bins + 1`` bin edges are returned,
59015900
consistently with :func:`numpy.histogram` for numpy version >=
59025901
1.3.
59035902
5904-
Unequally spaced bins are supported if `bins` is a sequence.
5903+
Unequally spaced bins are supported if *bins* is a sequence.
59055904
59065905
If Numpy 1.11 is installed, may also be ``'auto'``.
59075906
59085907
Default is taken from the rcParam ``hist.bins``.
59095908
59105909
range : tuple or None, optional
59115910
The lower and upper range of the bins. Lower and upper outliers
5912-
are ignored. If not provided, `range` is (x.min(), x.max()). Range
5913-
has no effect if `bins` is a sequence.
5911+
are ignored. If not provided, *range* is ``(x.min(), x.max())``.
5912+
Range has no effect if *bins* is a sequence.
59145913
5915-
If `bins` is a sequence or `range` is specified, autoscaling
5914+
If *bins* is a sequence or *range* is specified, autoscaling
59165915
is based on the specified bin range instead of the
59175916
range of x.
59185917
59195918
Default is ``None``
59205919
5921-
normed : boolean, optional
5922-
If `True`, the first element of the return tuple will
5920+
density : boolean, optional
5921+
If ``True``, the first element of the return tuple will
59235922
be the counts normalized to form a probability density, i.e.,
59245923
the area (or integral) under the histogram will sum to 1.
5925-
This is achieved dividing the count by the number of observations
5926-
times the bin width and *not* dividing by the total number
5927-
of observations. If `stacked` is also `True`, the sum of the
5928-
histograms is normalized to 1.
5924+
This is achieved by dividing the count by the number of
5925+
observations times the bin width and not dividing by the total
5926+
number of observations. If *stacked* is also ``True``, the sum of
5927+
the histograms is normalized to 1.
59295928
5930-
Default is ``False``
5929+
Default is ``None`` for both *normed* and *density*. If either is
5930+
set, then that value will be used. If neither are set, then the
5931+
args will be treated as ``False``.
5932+
5933+
If both *density* and *normed* are set an error is raised.
59315934
59325935
weights : (n, ) array_like or None, optional
5933-
An array of weights, of the same shape as `x`. Each value in `x`
5936+
An array of weights, of the same shape as *x*. Each value in *x*
59345937
only contributes its associated weight towards the bin count
5935-
(instead of 1). If `normed` is True, the weights are normalized,
5936-
so that the integral of the density over the range remains 1.
5938+
(instead of 1). If *normed* or *density* is ``True``,
5939+
the weights are normalized, so that the integral of the density
5940+
over the range remains 1.
59375941
59385942
Default is ``None``
59395943
59405944
cumulative : boolean, optional
5941-
If `True`, then a histogram is computed where each bin gives the
5945+
If ``True``, then a histogram is computed where each bin gives the
59425946
counts in that bin plus all bins for smaller values. The last bin
5943-
gives the total number of datapoints. If `normed` is also `True`
5944-
then the histogram is normalized such that the last bin equals 1.
5945-
If `cumulative` evaluates to less than 0 (e.g., -1), the direction
5946-
of accumulation is reversed. In this case, if `normed` is also
5947-
`True`, then the histogram is normalized such that the first bin
5948-
equals 1.
5947+
gives the total number of datapoints. If *normed* or *density*
5948+
is also ``True`` then the histogram is normalized such that the
5949+
last bin equals 1. If *cumulative* evaluates to less than 0
5950+
(e.g., -1), the direction of accumulation is reversed.
5951+
In this case, if *normed* and/or *density* is also ``True``, then
5952+
the histogram is normalized such that the first bin equals 1.
59495953
59505954
Default is ``False``
59515955
@@ -5991,22 +5995,23 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
59915995
59925996
rwidth : scalar or None, optional
59935997
The relative width of the bars as a fraction of the bin width. If
5994-
`None`, automatically compute the width.
5998+
``None``, automatically compute the width.
59955999
5996-
Ignored if `histtype` is 'step' or 'stepfilled'.
6000+
Ignored if *histtype* is 'step' or 'stepfilled'.
59976001
59986002
Default is ``None``
59996003
60006004
log : boolean, optional
6001-
If `True`, the histogram axis will be set to a log scale. If `log`
6002-
is `True` and `x` is a 1D array, empty bins will be filtered out
6003-
and only the non-empty (`n`, `bins`, `patches`) will be returned.
6005+
If ``True``, the histogram axis will be set to a log scale. If
6006+
*log* is ``True`` and *x* is a 1D array, empty bins will be
6007+
filtered out and only the non-empty ``(n, bins, patches)``
6008+
will be returned.
60046009
60056010
Default is ``False``
60066011
60076012
color : color or array_like of colors or None, optional
60086013
Color spec or sequence of color specs, one per dataset. Default
6009-
(`None`) uses the standard line color sequence.
6014+
(``None``) uses the standard line color sequence.
60106015
60116016
Default is ``None``
60126017
@@ -6018,21 +6023,22 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
60186023
default is ``None``
60196024
60206025
stacked : boolean, optional
6021-
If `True`, multiple data are stacked on top of each other If
6022-
`False` multiple data are aranged side by side if histtype is
6026+
If ``True``, multiple data are stacked on top of each other If
6027+
``False`` multiple data are aranged side by side if histtype is
60236028
'bar' or on top of each other if histtype is 'step'
60246029
60256030
Default is ``False``
60266031
60276032
Returns
60286033
-------
60296034
n : array or list of arrays
6030-
The values of the histogram bins. See **normed** and **weights**
6031-
for a description of the possible semantics. If input **x** is an
6032-
array, then this is an array of length **nbins**. If input is a
6033-
sequence arrays ``[data1, data2,..]``, then this is a list of
6034-
arrays with the values of the histograms for each of the arrays
6035-
in the same order.
6035+
The values of the histogram bins. See *normed* or *density*
6036+
and *weights* for a description of the possible semantics.
6037+
If input *x* is an array, then this is an array of length
6038+
*nbins*. If input is a sequence arrays
6039+
``[data1, data2,..]``, then this is a list of arrays with
6040+
the values of the histograms for each of the arrays in the
6041+
same order.
60366042
60376043
bins : array
60386044
The edges of the bins. Length nbins + 1 (nbins left edges and right
@@ -6054,8 +6060,8 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
60546060
Notes
60556061
-----
60566062
Until numpy release 1.5, the underlying numpy histogram function was
6057-
incorrect with `normed`=`True` if bin sizes were unequal. MPL
6058-
inherited that error. It is now corrected within MPL when using
6063+
incorrect with ``normed=True`` if bin sizes were unequal. MPL
6064+
inherited that error. It is now corrected within MPL when using
60596065
earlier numpy versions.
60606066
60616067
"""
@@ -6087,6 +6093,12 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
60876093
if histtype == 'barstacked' and not stacked:
60886094
stacked = True
60896095

6096+
if density is not None and normed is not None:
6097+
raise ValueError("kwargs 'density' and 'normed' cannot be used "
6098+
"simultaneously. "
6099+
"Please only use 'density', since 'normed'"
6100+
"will be deprecated.")
6101+
60906102
# process the unit information
60916103
self._process_unit_info(xdata=x, kwargs=kwargs)
60926104
x = self.convert_xunits(x)
@@ -6138,11 +6150,11 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
61386150
xmin = min(xmin, xi.min())
61396151
xmax = max(xmax, xi.max())
61406152
bin_range = (xmin, xmax)
6141-
6142-
# hist_kwargs = dict(range=range, normed=bool(normed))
6143-
# We will handle the normed kwarg within mpl until we
6144-
# get to the point of requiring numpy >= 1.5.
6145-
hist_kwargs = dict(range=bin_range)
6153+
density = bool(density) or bool(normed)
6154+
if density and not stacked:
6155+
hist_kwargs = dict(range=bin_range, density=density)
6156+
else:
6157+
hist_kwargs = dict(range=bin_range)
61466158

61476159
n = []
61486160
mlast = None
@@ -6153,7 +6165,7 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
61536165
m = m.astype(float) # causes problems later if it's an int
61546166
if mlast is None:
61556167
mlast = np.zeros(len(bins)-1, m.dtype)
6156-
if normed and not stacked:
6168+
if density and not stacked:
61576169
db = np.diff(bins)
61586170
m = (m.astype(float) / db) / m.sum()
61596171
if stacked:
@@ -6163,7 +6175,7 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
61636175
mlast[:] = m
61646176
n.append(m)
61656177

6166-
if stacked and normed:
6178+
if stacked and density:
61676179
db = np.diff(bins)
61686180
for m in n:
61696181
m[:] = (m.astype(float) / db) / n[-1].sum()
@@ -6172,7 +6184,7 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
61726184
if cbook.is_numlike(cumulative) and cumulative < 0:
61736185
slc = slice(None, None, -1)
61746186

6175-
if normed:
6187+
if density:
61766188
n = [(m * np.diff(bins))[slc].cumsum()[slc] for m in n]
61776189
else:
61786190
n = [m[slc].cumsum()[slc] for m in n]
@@ -6259,13 +6271,15 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
62596271
# Setting a minimum of 0 results in problems for log plots
62606272
if np.min(bottom) > 0:
62616273
minimum = np.min(bottom)
6262-
elif normed or weights is not None:
6263-
# For normed data, set to minimum data value / logbase
6274+
elif density or weights is not None:
6275+
# For data that is normed to form a probability density,
6276+
# set to minimum data value / logbase
62646277
# (gives 1 full tick-label unit for the lowest filled bin)
62656278
ndata = np.array(n)
62666279
minimum = (np.min(ndata[ndata > 0])) / logbase
62676280
else:
6268-
# For non-normed data, set the min to 1 / log base,
6281+
# For non-normed (density = False) data,
6282+
# set the min to 1 / log base,
62696283
# again so that there is 1 full tick-label unit
62706284
# for the lowest bin
62716285
minimum = 1.0 / logbase

lib/matplotlib/tests/test_axes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,27 @@ def test_hist_stacked_normed():
27762776
ax.hist((d1, d2), stacked=True, normed=True)
27772777

27782778

2779+
@image_comparison(baseline_images=['hist_stacked_normed'], extensions=['png'])
2780+
def test_hist_stacked_density():
2781+
# make some data
2782+
d1 = np.linspace(1, 3, 20)
2783+
d2 = np.linspace(0, 10, 50)
2784+
fig, ax = plt.subplots()
2785+
ax.hist((d1, d2), stacked=True, density=True)
2786+
2787+
2788+
@pytest.mark.parametrize('normed', [False, True])
2789+
@pytest.mark.parametrize('density', [False, True])
2790+
def test_hist_normed_density(normed, density):
2791+
# Normed and density should not be used simultaneously
2792+
d1 = np.linspace(1, 3, 20)
2793+
d2 = np.linspace(0, 10, 50)
2794+
fig, ax = plt.subplots()
2795+
# test that kwargs normed and density cannot be set both.
2796+
with pytest.raises(Exception):
2797+
ax.hist((d1, d2), stacked=True, normed=normed, density=density)
2798+
2799+
27792800
@image_comparison(baseline_images=['hist_step_bottom'], extensions=['png'],
27802801
remove_text=True)
27812802
def test_hist_step_bottom():

0 commit comments

Comments
 (0)