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

Skip to content

Commit 94bebac

Browse files
committed
Modified files to include facecolor and added test
Removed figsize from test Add multiple baseline image names Fixed test? Fixed test? Removed parametrize usage Add baseline images Add baseline image Fix docs Fix docs Deleted baseline images, changed test Fix test Fix test
1 parent 30a5a73 commit 94bebac

File tree

7 files changed

+70
-2967
lines changed

7 files changed

+70
-2967
lines changed
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
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 distributions. Each chart differentiates the histograms using various parameters: ax1 uses different linewidths, ax2 uses different hatches, ax3 uses different edgecolors, and ax4 uses different facecolors. Edgecolors have ax1 and ax3 as well to accentuate the differences between the histograms.
1813

1914
import matplotlib.pyplot as plt
2015
import numpy as np
2116
np.random.seed(19680801)
2217

23-
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(10,5))
18+
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(9, 9))
2419

2520
data1 = np.random.poisson(5, 1000)
2621
data2 = np.random.poisson(7, 1000)
2722
data3 = np.random.poisson(10, 1000)
2823

2924
labels = ["Data 1", "Data 2", "Data 3"]
3025

31-
ax1.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
32-
edgecolor=["red", "green", "blue"], linewidth=[1, 1.5, 2])
26+
ax1.hist([data1, data2, data3], bins=range(17), histtype="step", stacked=True,
27+
edgecolor=["red", "green", "blue"], linewidth=[1, 2, 3])
3328
ax1.set_title("Different linewidths")
34-
ax1.legend(labels, prop={"size": 8})
29+
ax1.legend(labels)
3530

3631
ax2.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
37-
edgecolor=["red", "green", "blue"], hatch=["/", ".", "*"])
32+
hatch=["/", ".", "*"])
3833
ax2.set_title("Different hatch patterns")
39-
ax2.legend(labels, prop={"size": 8})
34+
ax2.legend(labels)
4035

41-
ax3.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
36+
ax3.hist([data1, data2, data3], bins=range(17), histtype="bar", fill=False,
4237
edgecolor=["red", "green", "blue"], linestyle=["--", "-.", ":"])
4338
ax3.set_title("Different linestyles")
44-
ax3.legend(labels, prop={"size": 8})
39+
ax3.legend(labels)
40+
41+
ax4.hist([data1, data2, data3], bins=range(17), histtype="barstacked",
42+
facecolor=["red", "green", "blue"])
43+
ax4.set_title("Different facecolors")
44+
ax4.legend(labels)
4545

4646
plt.show()

galleries/examples/statistics/histogram_multihist.py

Lines changed: 21 additions & 12 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
@@ -65,15 +66,27 @@
6566

6667
edgecolors = ['green', 'red', 'blue']
6768

68-
ax.hist(
69-
x, n_bins, fill=False, histtype="step", stacked=True,
70-
edgecolor=edgecolors, label=edgecolors
71-
)
69+
ax.hist(x, n_bins, fill=False, histtype="step", stacked=True,
70+
edgecolor=edgecolors, label=edgecolors)
7271
ax.legend()
7372
ax.set_title('Stacked Steps with Edgecolors')
7473

7574
plt.show()
7675

76+
# %%
77+
# Histograms with Face-Colors
78+
# ...........................
79+
80+
fig, ax = plt.subplots()
81+
82+
facecolors = ['green', 'red', 'blue']
83+
84+
ax.hist(x, n_bins, histtype="barstacked", facecolor=facecolors, label=facecolors)
85+
ax.legend()
86+
ax.set_title("Bars with different Facecolors")
87+
88+
plt.show()
89+
7790
# %%
7891
# Histograms with Hatches
7992
# .......................
@@ -97,10 +110,8 @@
97110
linewidths = [1, 2, 3]
98111
edgecolors = ["green", "red", "blue"]
99112

100-
ax.hist(
101-
x, n_bins, fill=False, histtype="bar", linewidth=linewidths,
102-
edgecolor=edgecolors, label=linewidths,
103-
)
113+
ax.hist(x, n_bins, fill=False, histtype="bar", linewidth=linewidths,
114+
edgecolor=edgecolors, label=linewidths)
104115
ax.legend()
105116
ax.set_title("Bars with Linewidths")
106117

@@ -114,10 +125,8 @@
114125

115126
linestyles = ['-', ':', '--']
116127

117-
ax.hist(
118-
x, n_bins, fill=False, histtype='bar', linestyle=linestyles,
119-
edgecolor=edgecolors, label=linestyles
120-
)
128+
ax.hist(x, n_bins, fill=False, histtype='bar', linestyle=linestyles,
129+
edgecolor=edgecolors, label=linestyles)
121130
ax.legend()
122131
ax.set_title('Bars with Linestyles')
123132

lib/matplotlib/axes/_axes.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7214,18 +7214,14 @@ 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)))
7218-
if histtype.startswith('bar') or histtype == 'stepfilled':
7219-
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor', None)))
7220-
else:
7217+
if histtype == "step":
72217218
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor',
72227219
colors)))
7223-
if histtype.startswith('bar'):
7224-
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor',
7225-
colors)))
72267220
else:
7227-
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor',
7228-
colors)))
7221+
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get("edgecolor", None)))
7222+
7223+
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor', colors)))
7224+
hatches = itertools.cycle(np.atleast_1d(kwargs.get('hatch', None)))
72297225
linewidths = itertools.cycle(np.atleast_1d(kwargs.get('linewidth', None)))
72307226
linestyles = itertools.cycle(np.atleast_1d(kwargs.get('linestyle', None)))
72317227

Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)