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

Skip to content

Commit 30a5a73

Browse files
committed
Resolved edgecolor and facecolor setting
Minor fix Fix docs
1 parent f9b4d9a commit 30a5a73

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

doc/users/next_whats_new/histogram_vectorized_parameters.rst

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
Vectorize ``hatch``, ``edgecolor``, ``linewidth`` and ``linestyle`` in *hist* methods
2-
---------------------------------------------------------------------------------------
2+
-------------------------------------------------------------------------------------
33

4-
The parameters ``hatch``, ``edgecolor``, ``linewidth`` and ``linestyle`` of the `~matplotlib.axes.Axes.hist` method are now vectorized.
5-
This means that you can pass in unique parameters for each histogram that is generated when the input *x* has multiple datasets.
6-
Note that the ``facecolor`` parameter is not vectorized, but the required behavior can be achieved by passing a list of colors to the ``color`` parameter.
4+
The parameters ``hatch``, ``edgecolor``, ``linewidth`` and ``linestyle`` of the
5+
`~matplotlib.axes.Axes.hist` method are now vectorized.
6+
This means that you can pass in unique parameters for each histogram that is generated
7+
when the input *x* has multiple datasets.
8+
Note that the ``facecolor`` parameter is not vectorized, but the required behavior can
9+
be achieved by passing a list of colors to the ``color`` parameter.
710

811
.. plot::
912
:include-source: true
10-
:alt: Three charts, identified as ax1, ax2 and ax3, show a stacking of three histograms representing Poisson distributions. The histograms in ax1, ax2, and ax3 are differentiated by linewidths, hatches and linestyles, respectively. In ax1, ax2, and ax3, each histogram is bordered by a different color.
13+
:alt: Three charts, identified as ax1, ax2 and ax3, show a stacking of three
14+
histograms representing Poisson distributions. The histograms in ax1, ax2,
15+
and ax3 are differentiated by linewidths, hatches and linestyles,
16+
respectively. In ax1, ax2 and ax3, each histogram is bordered by a different
17+
color.
1118

1219
import matplotlib.pyplot as plt
1320
import numpy as np
@@ -21,15 +28,18 @@ Note that the ``facecolor`` parameter is not vectorized, but the required behavi
2128

2229
labels = ["Data 1", "Data 2", "Data 3"]
2330

24-
ax1.hist([data1, data2, data3], bins=range(17), histtype="barstacked", edgecolor=["red", "green", "blue"], linewidth=[1, 1.5, 2])
31+
ax1.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
32+
edgecolor=["red", "green", "blue"], linewidth=[1, 1.5, 2])
2533
ax1.set_title("Different linewidths")
2634
ax1.legend(labels, prop={"size": 8})
2735

28-
ax2.hist([data1, data2, data3], bins=range(17), histtype="barstacked", edgecolor=["red", "green", "blue"], hatch=["/", ".", "*"])
36+
ax2.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
37+
edgecolor=["red", "green", "blue"], hatch=["/", ".", "*"])
2938
ax2.set_title("Different hatch patterns")
3039
ax2.legend(labels, prop={"size": 8})
3140

32-
ax3.hist([data1, data2, data3], bins=range(17), histtype="barstacked", edgecolor=["red", "green", "blue"], linestyle=["--", "-.", ":"])
41+
ax3.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
42+
edgecolor=["red", "green", "blue"], linestyle=["--", "-.", ":"])
3343
ax3.set_title("Different linestyles")
3444
ax3.legend(labels, prop={"size": 8})
3545

galleries/examples/statistics/histogram_multihist.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
plt.show()
7676

77-
77+
# %%
7878
# Histograms with Hatches
7979
# .......................
8080

@@ -88,7 +88,7 @@
8888

8989
plt.show()
9090

91-
91+
# %%
9292
# Histograms with Linewidths
9393
# ..........................
9494

@@ -106,9 +106,9 @@
106106

107107
plt.show()
108108

109-
109+
# %%
110110
# Histograms with LineStyles
111-
# ...........................
111+
# ..........................
112112

113113
fig, ax = plt.subplots()
114114

lib/matplotlib/axes/_axes.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6938,8 +6938,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
69386938
69396939
**kwargs
69406940
`~matplotlib.patches.Patch` properties. The following properties
6941-
additionally accepts a sequence of properties values corresponding
6942-
to the datasets in *x*:
6941+
additionally accept a sequence of values corresponding to the
6942+
datasets in *x*:
69436943
*edgecolors*, *linewidths*, *linestyles*, *hatches*.
69446944
69456945
See Also
@@ -7215,19 +7215,29 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
72157215
labels = [] if label is None else np.atleast_1d(np.asarray(label, str))
72167216

72177217
hatches = itertools.cycle(np.atleast_1d(kwargs.get('hatch', None)))
7218-
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor', None)))
7218+
if histtype.startswith('bar') or histtype == 'stepfilled':
7219+
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor', None)))
7220+
else:
7221+
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor',
7222+
colors)))
7223+
if histtype.startswith('bar'):
7224+
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor',
7225+
colors)))
7226+
else:
7227+
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor',
7228+
colors)))
72197229
linewidths = itertools.cycle(np.atleast_1d(kwargs.get('linewidth', None)))
72207230
linestyles = itertools.cycle(np.atleast_1d(kwargs.get('linestyle', None)))
72217231

72227232
for patch, lbl in itertools.zip_longest(patches, labels):
72237233
if patch:
72247234
p = patch[0]
7225-
if 'edgecolor' in kwargs:
7226-
kwargs['edgecolor'] = next(edgecolors)
72277235
kwargs.update({
72287236
'hatch': next(hatches),
72297237
'linewidth': next(linewidths),
72307238
'linestyle': next(linestyles),
7239+
'edgecolor': next(edgecolors),
7240+
'facecolor': next(facecolors),
72317241
})
72327242
p._internal_update(kwargs)
72337243
if lbl is not None:

0 commit comments

Comments
 (0)