Skip to content

Commit 1295778

Browse files
committed
Add rcParam hist.edgecolor
1 parent 2698288 commit 1295778

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
New rcparam ``hist.edgecolor``
2+
``````````````````````````````
3+
The new rcparam ``hist.edgecolor`` allows to expicitly define an edge color
4+
for the histogram bars. Previously, this could only be achieved by setting
5+
``patch.edgecolor`` and ``patch.force_edgecolor``, which may have affected
6+
other plot elements unwantedly.

lib/matplotlib/axes/_axes.py

+32-8
Original file line numberDiff line numberDiff line change
@@ -6335,8 +6335,8 @@ def table(self, **kwargs):
63356335
@_preprocess_data(replace_names=["x", 'weights'], label_namer="x")
63366336
def hist(self, x, bins=None, range=None, density=None, weights=None,
63376337
cumulative=False, bottom=None, histtype='bar', align='mid',
6338-
orientation='vertical', rwidth=None, log=False,
6339-
color=None, label=None, stacked=False, normed=None,
6338+
orientation='vertical', rwidth=None, log=False, color=None,
6339+
edgecolor=None, label=None, stacked=False, normed=None,
63406340
**kwargs):
63416341
"""
63426342
Plot a histogram.
@@ -6494,6 +6494,12 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64946494
64956495
Default is ``None``
64966496
6497+
edgecolor : color or array_like of colors or None, optional
6498+
Color spec or sequence of color specs, one per dataset. Default
6499+
(``None``) :rc:`hist.edgecolor`. If that is ``None`` as well, the
6500+
default patch settings are used (:rc:`patch.edgecolor` and
6501+
:rc:`patch.force_edgecolor`).
6502+
64976503
label : str or None, optional
64986504
String, or sequence of strings to match multiple datasets. Bar
64996505
charts yield multiple patches per dataset, but only the first gets
@@ -6546,6 +6552,8 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
65466552
.. [Notes section required for data comment. See #10189.]
65476553
65486554
"""
6555+
kwargs = cbook.normalize_kwargs(kwargs, mpatches.Patch._alias_map)
6556+
65496557
# Avoid shadowing the builtin.
65506558
bin_range = range
65516559
from builtins import range
@@ -6619,10 +6627,24 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
66196627
else:
66206628
color = mcolors.to_rgba_array(color)
66216629
if len(color) != nx:
6622-
error_message = (
6630+
raise ValueError(
66236631
"color kwarg must have one color per data set. %d data "
66246632
"sets and %d colors were provided" % (nx, len(color)))
6625-
raise ValueError(error_message)
6633+
6634+
if edgecolor is None:
6635+
edgecolor = rcParams['hist.edgecolor']
6636+
if edgecolor is None:
6637+
edgecolor = [None] * nx
6638+
else:
6639+
edgecolor = mcolors.to_rgba_array(edgecolor)
6640+
if len(edgecolor) != nx:
6641+
raise ValueError(
6642+
"edgecolor kwarg must have one color per data set. "
6643+
"%d data sets and %d colors were provided" % (
6644+
nx, len(edgecolor)))
6645+
6646+
if rcParams['hist.linewidth'] is not None:
6647+
kwargs.setdefault('linewidth', rcParams['hist.linewidth'])
66266648

66276649
# If bins are not specified either explicitly or via range,
66286650
# we need to figure out the range required for all datasets,
@@ -6715,7 +6737,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
67156737
_barfunc = self.bar
67166738
bottom_kwarg = 'bottom'
67176739

6718-
for m, c in zip(tops, color):
6740+
for m, c, ec in zip(tops, color, edgecolor):
67196741
if bottom is None:
67206742
bottom = np.zeros(len(m))
67216743
if stacked:
@@ -6724,7 +6746,8 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
67246746
height = m
67256747
patch = _barfunc(bins[:-1]+boffset, height, width,
67266748
align='center', log=log,
6727-
color=c, **{bottom_kwarg: bottom})
6749+
color=c, edgecolor=ec,
6750+
**{bottom_kwarg: bottom})
67286751
patches.append(patch)
67296752
if stacked:
67306753
bottom[:] = m
@@ -6805,12 +6828,13 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
68056828
# add patches in reverse order so that when stacking,
68066829
# items lower in the stack are plotted on top of
68076830
# items higher in the stack
6808-
for x, y, c in reversed(list(zip(xvals, yvals, color))):
6831+
for x, y, c, ec in reversed(list(zip(xvals, yvals, color,
6832+
edgecolor))):
68096833
patches.append(self.fill(
68106834
x[:split], y[:split],
68116835
closed=True if fill else None,
68126836
facecolor=c,
6813-
edgecolor=None if fill else c,
6837+
edgecolor=ec if fill else c,
68146838
fill=fill if fill else None))
68156839
for patch_list in patches:
68166840
for patch in patch_list:

lib/matplotlib/rcsetup.py

+8
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ def validate_color_or_auto(s):
353353
return validate_color(s)
354354

355355

356+
def validate_color_or_None(s):
357+
if s is None:
358+
return s
359+
return validate_color(s)
360+
361+
356362
def validate_color_for_prop_cycle(s):
357363
# Special-case the N-th color cycle syntax, this obviously can not
358364
# go in the color cycle.
@@ -1049,6 +1055,8 @@ def _validate_linestyle(ls):
10491055

10501056
## Histogram properties
10511057
'hist.bins': [10, validate_hist_bins],
1058+
'hist.edgecolor': [None, validate_color_or_None],
1059+
'hist.linewidth': [None, validate_float_or_None],
10521060

10531061
## Boxplot properties
10541062
'boxplot.notch': [False, validate_bool],

lib/matplotlib/tests/test_axes.py

+18
Original file line numberDiff line numberDiff line change
@@ -3151,6 +3151,24 @@ def test_hist_labels():
31513151
assert l[2][0].get_label() == '00'
31523152

31533153

3154+
@check_figures_equal(extensions=['png'])
3155+
def test_hist_bar_rc(fig_test, fig_ref):
3156+
with plt.rc_context({'hist.edgecolor': 'darkblue',
3157+
'hist.linewidth': 5}):
3158+
fig_test.subplots().hist([1, 5, 3], histtype='bar')
3159+
fig_ref.subplots().hist([1, 5, 3], histtype='bar',
3160+
edgecolor='darkblue', linewidth=5)
3161+
3162+
3163+
@check_figures_equal(extensions=['png'])
3164+
def test_hist_stepfilled_rc(fig_test, fig_ref):
3165+
with plt.rc_context({'hist.edgecolor': 'darkblue',
3166+
'hist.linewidth': 5}):
3167+
fig_test.subplots().hist([1, 5, 3], histtype='stepfilled')
3168+
fig_ref.subplots().hist([1, 5, 3], histtype='stepfilled',
3169+
edgecolor='darkblue', linewidth=5)
3170+
3171+
31543172
@image_comparison(baseline_images=['transparent_markers'], remove_text=True)
31553173
def test_transparent_markers():
31563174
np.random.seed(0)

0 commit comments

Comments
 (0)