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

ENH/API: allow hist.bins to follow the numpy default #16471

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
Loading
from
Draft
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions 18 lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6608,6 +6608,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,

if bins is None:
bins = rcParams['hist.bins']
if isinstance(bins, str) and bins == 'np_default':
bins = None

# Validate string inputs here to avoid cluttering subsequent code.
cbook._check_in_list(['bar', 'barstacked', 'step', 'stepfilled'],
Expand All @@ -6633,7 +6635,7 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
if bin_range is not None:
bin_range = self.convert_xunits(bin_range)

if not cbook.is_scalar_or_string(bins):
if bins is not None and not cbook.is_scalar_or_string(bins):
bins = self.convert_xunits(bins)

# We need to do to 'weights' what was done to 'x'
Expand Down Expand Up @@ -6687,8 +6689,12 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
_w = np.concatenate(w)
else:
_w = None
bins = np.histogram_bin_edges(
np.concatenate(x), bins, bin_range, _w)
nbe_kwargs = {'range': bin_range,
'weights': _w}
if bins is not None:
nbe_kwargs['bins'] = bins
bins = np.histogram_bin_edges(np.concatenate(x),
**nbe_kwargs)
else:
hist_kwargs['range'] = bin_range

Expand All @@ -6702,7 +6708,11 @@ def hist(self, x, bins=None, range=None, density=False, 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)
h_kwargs = {'weights': w[i], **hist_kwargs}
# This awkward way of calling
if bins is not None:
h_kwargs['bins'] = bins
m, bins = np.histogram(x[i], **h_kwargs)
tops.append(m)
tops = np.array(tops, float) # causes problems later if it's an int
if stacked:
Expand Down
6 changes: 3 additions & 3 deletions 6 lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ def validate_cycler(s):


def validate_hist_bins(s):
valid_strs = ["auto", "sturges", "fd", "doane", "scott", "rice", "sqrt"]
valid_strs = ["auto", "sturges", "fd", "doane", "scott", "rice", "sqrt", "np_default"]
if isinstance(s, str) and s in valid_strs:
return s
try:
Expand All @@ -962,7 +962,7 @@ def validate_hist_bins(s):
return validate_floatlist(s)
except ValueError:
pass
raise ValueError("'hist.bins' must be one of {}, an int or"
raise ValueError("'hist.bins' must be one of {}, None, an int or"
" a sequence of floats".format(valid_strs))


Expand Down Expand Up @@ -1052,7 +1052,7 @@ def validate_webagg_address(s):
'hatch.linewidth': [1.0, validate_float],

## Histogram properties
'hist.bins': [10, validate_hist_bins],
'hist.bins': ['np_default', validate_hist_bins],

## Boxplot properties
'boxplot.notch': [False, validate_bool],
Expand Down
14 changes: 14 additions & 0 deletions 14 lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6187,6 +6187,20 @@ def test_hist_auto_bins():
assert bins[-1] >= 6



def test_hist_np_default_bins():

with plt.rc_context({'hist.bins': 'np_default'}):
_, edges, _ = plt.hist([[1, 2, 3], [3, 4, 5, 6]])
# this encodes that numpy's default is bins=10 -> 11 edges
assert len(edges) == 11

_, bins, _ = plt.hist([[1, 2, 3], [3, 4, 5, 6]], bins='np_default')
# this encodes that numpy's default is bins=10 -> 11 edges
len(edges) == 11



def test_hist_nan_data():
fig, (ax1, ax2) = plt.subplots(2)

Expand Down
1 change: 1 addition & 0 deletions 1 lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def generate_validator_testcases(valid):
'success': (('auto', 'auto'),
('fd', 'fd'),
('10', 10),
('np_default', 'np_default'),
('1, 2, 3', [1, 2, 3]),
([1, 2, 3], [1, 2, 3]),
(np.arange(15), np.arange(15))
Expand Down
2 changes: 1 addition & 1 deletion 2 matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@
## ***************************************************************************
## * HISTOGRAM PLOTS *
## ***************************************************************************
#hist.bins : 10 ## The default number of histogram bins or 'auto'.
#hist.bins : np_default ## The default number of histogram bins or 'auto'.


## ***************************************************************************
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.