Skip to content

Add ishistogammed option to the hist plotting method #12306

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

Closed
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
17 changes: 16 additions & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6256,6 +6256,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
cumulative=False, bottom=None, histtype='bar', align='mid',
orientation='vertical', rwidth=None, log=False,
color=None, label=None, stacked=False, normed=None,
ishistogrammed=False,
**kwargs):
"""
Plot a histogram.
Expand Down Expand Up @@ -6430,6 +6431,13 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
normed : bool, optional
Deprecated; use the density keyword argument instead.

ishistogrammed : bool, optional
If ``True``, interpret the input data ``x`` as already histogrammed
data, preventing this method from preforming the histogramming. If
``True``, the argument ``bins`` must also be provided.

Default is ``False``

Returns
-------
n : array or list of arrays
Expand Down Expand Up @@ -6497,6 +6505,10 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
cbook.warn_deprecated("2.1", name="'normed'", obj_type="kwarg",
alternative="'density'", removal="3.1")

if ishistogrammed and bins is None:
raise ValueError("'bins' must be passed if 'ishistogrammed' is "
"True.")

# basic input validation
input_empty = np.size(x) == 0
# Massage 'x' for processing.
Expand Down Expand Up @@ -6565,7 +6577,10 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
for i in range(nx):
# this will automatically overwrite bins,
# so that each histogram uses the same bins
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
if ishistogrammed:
m, bins = x[i], bins
else:
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
m = m.astype(float) # causes problems later if it's an int
if mlast is None:
mlast = np.zeros(len(bins)-1, m.dtype)
Expand Down