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

Skip to content

Commit 36e8a64

Browse files
committed
Modified files to include facecolor and added test
1 parent fc6a1ba commit 36e8a64

File tree

4 files changed

+70
-51
lines changed

4 files changed

+70
-51
lines changed
Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
1-
Vectorize ``hatch``, ``edgecolor``, ``linewidth`` and ``linestyle`` in *hist* methods
2-
-------------------------------------------------------------------------------------
1+
Vectorize ``hatch``, ``edgecolor``, ``facecolor``, ``linewidth`` and ``linestyle`` in *hist* methods
2+
----------------------------------------------------------------------------------------------------
33

4-
The parameters ``hatch``, ``edgecolor``, ``linewidth`` and ``linestyle`` of the
5-
`~matplotlib.axes.Axes.hist` method are now vectorized.
4+
The parameters ``hatch``, ``edgecolor``, ``facecolor``, ``linewidth`` and ``linestyle``
5+
of the `~matplotlib.axes.Axes.hist` method are now vectorized.
66
This means that you can pass in unique parameters for each histogram that is generated
77
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.
8+
109

1110
.. plot::
1211
:include-source: true
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.
12+
:alt: Four charts, each displaying stacked histograms of three Poisson
13+
distributions. Each chart differentiates the histograms using various
14+
parameters: ax1 uses different linewidths, ax2 uses different hatches, ax3
15+
uses different edgecolors, and ax4 uses different facecolors. Edgecolors have
16+
ax1 and ax3 as well to accentuate the differences between the histograms.
17+
18+
import matplotlib.pyplot as plt
19+
import numpy as np
20+
np.random.seed(19680801)
1821

19-
import matplotlib.pyplot as plt
20-
import numpy as np
21-
np.random.seed(19680801)
22+
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(9, 9))
2223

23-
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(10,5))
24+
data1 = np.random.poisson(5, 1000)
25+
data2 = np.random.poisson(7, 1000)
26+
data3 = np.random.poisson(10, 1000)
2427

25-
data1 = np.random.poisson(5, 1000)
26-
data2 = np.random.poisson(7, 1000)
27-
data3 = np.random.poisson(10, 1000)
28+
labels = ["Data 1", "Data 2", "Data 3"]
2829

29-
labels = ["Data 1", "Data 2", "Data 3"]
30+
ax1.hist([data1, data2, data3], bins=range(17), histtype="step", stacked=True,
31+
edgecolor=["red", "green", "blue"], linewidth=[1, 2, 3])
32+
ax1.set_title("Different linewidths")
33+
ax1.legend(labels)
3034

31-
ax1.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
32-
edgecolor=["red", "green", "blue"], linewidth=[1, 1.5, 2])
33-
ax1.set_title("Different linewidths")
34-
ax1.legend(labels, prop={"size": 8})
35+
ax2.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
36+
hatch=["/", ".", "*"])
37+
ax2.set_title("Different hatch patterns")
38+
ax2.legend(labels)
3539

36-
ax2.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
37-
edgecolor=["red", "green", "blue"], hatch=["/", ".", "*"])
38-
ax2.set_title("Different hatch patterns")
39-
ax2.legend(labels, prop={"size": 8})
40+
ax3.hist([data1, data2, data3], bins=range(17), histtype="bar", fill=False,
41+
edgecolor=["red", "green", "blue"], linestyle=["--", "-.", ":"])
42+
ax3.set_title("Different linestyles")
43+
ax3.legend(labels)
4044

41-
ax3.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
42-
edgecolor=["red", "green", "blue"], linestyle=["--", "-.", ":"])
43-
ax3.set_title("Different linestyles")
44-
ax3.legend(labels, prop={"size": 8})
45+
ax4.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
46+
facecolor=["red", "green", "blue"])
47+
ax4.set_title("Different facecolors")
48+
ax4.legend(labels)
4549

46-
plt.show()
50+
plt.show()

galleries/examples/statistics/histogram_multihist.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
# Plotting bar charts with datasets differentiated using:
5454
#
5555
# * edgecolors
56+
# * facecolors
5657
# * hatches
5758
# * linewidths
5859
# * linestyles
@@ -74,6 +75,20 @@
7475

7576
plt.show()
7677

78+
# %%
79+
# Histograms with Face-Colors
80+
# ...........................
81+
82+
fig, ax = plt.subplots()
83+
84+
facecolors = ['green', 'red', 'blue']
85+
86+
ax.hist(x, n_bins, histtype="barstacked", facecolor=facecolors, label=facecolors)
87+
ax.legend()
88+
ax.set_title("Bars with different Facecolors")
89+
90+
plt.show()
91+
7792
# %%
7893
# Histograms with Hatches
7994
# .......................

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7214,18 +7214,13 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
72147214
# cast each element to str, but keep a single str as it.
72157215
labels = [] if label is None else np.atleast_1d(np.asarray(label, str))
72167216

7217-
hatches = itertools.cycle(np.atleast_1d(kwargs.get('hatch', None)))
72187217
if histtype.startswith('bar') or histtype == 'stepfilled':
72197218
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor', None)))
72207219
else:
72217220
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor',
72227221
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)))
7222+
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor', colors)))
7223+
hatches = itertools.cycle(np.atleast_1d(kwargs.get('hatch', None)))
72297224
linewidths = itertools.cycle(np.atleast_1d(kwargs.get('linewidth', None)))
72307225
linestyles = itertools.cycle(np.atleast_1d(kwargs.get('linestyle', None)))
72317226

lib/matplotlib/tests/test_axes.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4603,22 +4603,27 @@ def test_hist_stacked_bar():
46034603
ax.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), ncols=1)
46044604

46054605

4606-
@image_comparison(['hist_vectorized_params'], extensions=["png", "pdf", "svg"],
4607-
remove_text=True)
4608-
def test_hist_vectorized_params():
4609-
fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2)
4606+
@pytest.mark.parametrize('histtype, fill', [('bar', False), ('bar', True),
4607+
('step', False), ('step', True)])
4608+
@image_comparison(['hist_vectorized_params'], extensions=["png"], remove_text=True)
4609+
def test_hist_vectorized_params(histtype, fill):
4610+
fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5, figsize=(18, 5))
46104611

46114612
np.random.seed(19680801)
46124613
x = [np.random.randn(n) for n in [2000, 5000, 10000]]
46134614

4614-
ax0.hist(x, bins=10, histtype="barstacked", edgecolor=["red", "black", "blue"],
4615-
linewidth=[1, 1.2, 1.5], hatch=["/", "\\", "."])
4616-
ax1.hist(x, bins=10, histtype="barstacked", linewidth=[1, 1.2, 1.5],
4617-
hatch=["/", "\\", "."], linestyle=["-", "--", ":"])
4618-
ax2.hist(x, bins=10, histtype="barstacked", edgecolor=["red", "black", "blue"],
4619-
hatch=["/", "\\", "."], linestyle=["-", "--", ":"])
4620-
ax3.hist(x, bins=10, histtype="barstacked", edgecolor=["red", "black", "blue"],
4621-
linewidth=[1, 1.2, 1.5], linestyle=["-", "--", ":"])
4615+
# Only linestyle
4616+
ax1.hist(x, bins=10, histtype=histtype, fill=fill, linestyle=["-", "--", ":"])
4617+
# Only edgecolor
4618+
ax2.hist(x, bins=10, histtype=histtype, fill=fill,
4619+
edgecolor=["red", "black", "blue"])
4620+
# Only facecolor
4621+
ax3.hist(x, bins=10, histtype=histtype, fill=fill,
4622+
facecolor=["blue", "yellow", "brown"])
4623+
# Only linewidth
4624+
ax4.hist(x, bins=10, histtype=histtype, fill=fill, linewidth=[1, 1.5, 2])
4625+
# Only hatch
4626+
ax5.hist(x, bins=10, histtype=histtype, fill=fill, hatch=["/", "\\", "."])
46224627

46234628

46244629
def test_hist_barstacked_bottom_unchanged():

0 commit comments

Comments
 (0)