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

Skip to content

Commit 51aea55

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

File tree

4 files changed

+97
-16
lines changed

4 files changed

+97
-16
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
New rcparams ``hist.edgecolor`` and ``hist.linewidth``
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.
7+
8+
Additionally, the new rcparam ``hist.linewidth`` specifies the linewdith of
9+
the bar edges.

lib/matplotlib/axes/_axes.py

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6335,10 +6335,10 @@ 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+
label=None, stacked=False, normed=None,
63406340
**kwargs):
6341-
"""
6341+
r"""
63426342
Plot a histogram.
63436343
63446344
Compute and draw the histogram of *x*. The return value is a
@@ -6440,7 +6440,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64406440
64416441
Default is ``None``
64426442
6443-
histtype : {'bar', 'barstacked', 'step', 'stepfilled'}, optional
6443+
histtype : {'bar', 'barstacked', 'step', 'stepfilled'}, optional
64446444
The type of histogram to draw.
64456445
64466446
- 'bar' is a traditional bar-type histogram. If multiple data
@@ -6488,12 +6488,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64886488
64896489
Default is ``False``
64906490
6491-
color : color or array_like of colors or None, optional
6492-
Color spec or sequence of color specs, one per dataset. Default
6493-
(``None``) uses the standard line color sequence.
6494-
6495-
Default is ``None``
6496-
64976491
label : str or None, optional
64986492
String, or sequence of strings to match multiple datasets. Bar
64996493
charts yield multiple patches per dataset, but only the first gets
@@ -6535,6 +6529,39 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
65356529
65366530
Other Parameters
65376531
----------------
6532+
color : color or array_like of colors or None, optional
6533+
The meaning of the parameter depends on *histtype*:
6534+
6535+
- For filled *histtype*\s ('bar', 'barstacked' and 'stepfilled'):
6536+
6537+
The facecolor of the bars. If providing multiple datasets to
6538+
*x*, there must be one color per dataset. If *None* the
6539+
standard line color sequence is used.
6540+
6541+
The *color* parameter is overridden if *facecolor* is given.
6542+
6543+
- For *histtype* 'step':
6544+
6545+
The linecolor of the step line. If None the standard line color
6546+
sequence is used.
6547+
6548+
Default: *None*
6549+
6550+
edgecolor : color or array_like of colors, optional
6551+
The edgecolor of the bars. If providing multiple datasets to
6552+
*x*, you can either pass a single edgecolor for all datasets or
6553+
one edgecolor per dataset. If not given, use :rc:`hist.edgecolor`.
6554+
If that is *None*, the default patch settings are used
6555+
(:rc:`patch.edgecolor` and :rc:`patch.force_edgecolor`).
6556+
6557+
Note: *edgecolor* is ignored for *histtype* 'step'. Use *color*
6558+
in this case.
6559+
6560+
linewidth : float, optional
6561+
The linewidth of the bar edges. If not given, use
6562+
:rc:`hist.linewidth`. If that is *None*, the default
6563+
:rc:`patch.linewidth` is used.
6564+
65386565
**kwargs : `~matplotlib.patches.Patch` properties
65396566
65406567
See also
@@ -6546,6 +6573,8 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
65466573
.. [Notes section required for data comment. See #10189.]
65476574
65486575
"""
6576+
kwargs = cbook.normalize_kwargs(kwargs, mpatches.Patch._alias_map)
6577+
65496578
# Avoid shadowing the builtin.
65506579
bin_range = range
65516580
from builtins import range
@@ -6619,10 +6648,25 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
66196648
else:
66206649
color = mcolors.to_rgba_array(color)
66216650
if len(color) != nx:
6622-
error_message = (
6651+
raise ValueError(
66236652
"color kwarg must have one color per data set. %d data "
66246653
"sets and %d colors were provided" % (nx, len(color)))
6625-
raise ValueError(error_message)
6654+
6655+
edgecolor = kwargs.pop('edgecolor', rcParams['hist.edgecolor'])
6656+
if edgecolor is None:
6657+
edgecolor = [None] * nx
6658+
else:
6659+
edgecolor = mcolors.to_rgba_array(edgecolor)
6660+
if len(edgecolor) == 1:
6661+
edgecolor = np.repeat(nx, axis=0)
6662+
elif len(edgecolor) != nx:
6663+
raise ValueError(
6664+
"edgecolor kwarg must have one color per data set. "
6665+
"%d data sets and %d colors were provided" % (
6666+
nx, len(edgecolor)))
6667+
6668+
if rcParams['hist.linewidth'] is not None:
6669+
kwargs.setdefault('linewidth', rcParams['hist.linewidth'])
66266670

66276671
# If bins are not specified either explicitly or via range,
66286672
# we need to figure out the range required for all datasets,
@@ -6715,7 +6759,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
67156759
_barfunc = self.bar
67166760
bottom_kwarg = 'bottom'
67176761

6718-
for m, c in zip(tops, color):
6762+
for m, c, ec in zip(tops, color, edgecolor):
67196763
if bottom is None:
67206764
bottom = np.zeros(len(m))
67216765
if stacked:
@@ -6724,7 +6768,8 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
67246768
height = m
67256769
patch = _barfunc(bins[:-1]+boffset, height, width,
67266770
align='center', log=log,
6727-
color=c, **{bottom_kwarg: bottom})
6771+
color=c, edgecolor=ec,
6772+
**{bottom_kwarg: bottom})
67286773
patches.append(patch)
67296774
if stacked:
67306775
bottom[:] = m
@@ -6805,12 +6850,13 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
68056850
# add patches in reverse order so that when stacking,
68066851
# items lower in the stack are plotted on top of
68076852
# items higher in the stack
6808-
for x, y, c in reversed(list(zip(xvals, yvals, color))):
6853+
for x, y, c, ec in reversed(list(zip(xvals, yvals, color,
6854+
edgecolor))):
68096855
patches.append(self.fill(
68106856
x[:split], y[:split],
68116857
closed=True if fill else None,
68126858
facecolor=c,
6813-
edgecolor=None if fill else c,
6859+
edgecolor=ec if fill else c,
68146860
fill=fill if fill else None))
68156861
for patch_list in patches:
68166862
for patch in patch_list:

lib/matplotlib/rcsetup.py

Lines changed: 8 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
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)