Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Add support for multiple hatches, edgecolors and linewidths in histograms#28073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
1a6ec0f
0e52daa
34df06a
4fcb709
347ce22
d7ffeaa
9024be4
3bfca5f
6029e32
0349e7d
d1a8079
9a813f4
43ce57d
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Vectorized ``hist`` style parameters | ||
------------------------------------ | ||
The parameters *hatch*, *edgecolor*, *facecolor*, *linewidth* and *linestyle* | ||
of the `~matplotlib.axes.Axes.hist` method are now vectorized. | ||
This means that you can pass in individual parameters for each histogram | ||
when the input *x* has multiple datasets. | ||
.. plot:: | ||
:include-source: true | ||
:alt: Four charts, each displaying stacked histograms of three Poisson distributions. Each chart differentiates the histograms using various parameters: top left uses different linewidths, top right uses different hatches, bottom left uses different edgecolors, and bottom right uses different facecolors. Each histogram on the left side also has a different edgecolor. | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
np.random.seed(19680801) | ||
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(9, 9)) | ||
data1 = np.random.poisson(5, 1000) | ||
data2 = np.random.poisson(7, 1000) | ||
data3 = np.random.poisson(10, 1000) | ||
labels = ["Data 1", "Data 2", "Data 3"] | ||
ax1.hist([data1, data2, data3], bins=range(17), histtype="step", stacked=True, | ||
edgecolor=["red", "green", "blue"], linewidth=[1, 2, 3]) | ||
ax1.set_title("Different linewidths") | ||
ax1.legend(labels) | ||
ax2.hist([data1, data2, data3], bins=range(17), histtype="barstacked", | ||
hatch=["/", ".", "*"]) | ||
ax2.set_title("Different hatch patterns") | ||
ax2.legend(labels) | ||
ax3.hist([data1, data2, data3], bins=range(17), histtype="bar", fill=False, | ||
edgecolor=["red", "green", "blue"], linestyle=["--", "-.", ":"]) | ||
ax3.set_title("Different linestyles") | ||
ax3.legend(labels) | ||
ax4.hist([data1, data2, data3], bins=range(17), histtype="barstacked", | ||
facecolor=["red", "green", "blue"]) | ||
ax4.set_title("Different facecolors") | ||
ax4.legend(labels) | ||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -6937,7 +6937,13 @@ def hist(self, x, bins=None, range=None, density=False, weights=None, | ||
DATA_PARAMETER_PLACEHOLDER | ||
**kwargs | ||
`~matplotlib.patches.Patch` properties. The following properties | ||
additionally accept a sequence of values corresponding to the | ||
datasets in *x*: | ||
*edgecolors*, *facecolors*, *lines*, *linestyles*, *hatches*. | ||
.. versionadded:: 3.10 | ||
Impaler343 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
Impaler343 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Allowing sequences of values in above listed Patch properties. | ||
See Also | ||
-------- | ||
@@ -7210,15 +7216,35 @@ def hist(self, x, bins=None, range=None, density=False, weights=None, | ||
# If None, make all labels None (via zip_longest below); otherwise, | ||
# cast each element to str, but keep a single str as it. | ||
labels = [] if label is None else np.atleast_1d(np.asarray(label, str)) | ||
if histtype == "step": | ||
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor', | ||
colors))) | ||
Impaler343 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
else: | ||
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get("edgecolor", None))) | ||
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor', colors))) | ||
hatches = itertools.cycle(np.atleast_1d(kwargs.get('hatch', None))) | ||
linewidths = itertools.cycle(np.atleast_1d(kwargs.get('linewidth', None))) | ||
linestyles = itertools.cycle(np.atleast_1d(kwargs.get('linestyle', None))) | ||
for patch, lbl in itertools.zip_longest(patches, labels): | ||
if not patch: | ||
continue | ||
p = patch[0] | ||
Impaler343 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
kwargs.update({ | ||
'hatch': next(hatches), | ||
'linewidth': next(linewidths), | ||
'linestyle': next(linestyles), | ||
'edgecolor': next(edgecolors), | ||
'facecolor': next(facecolors), | ||
}) | ||
p._internal_update(kwargs) | ||
if lbl is not None: | ||
p.set_label(lbl) | ||
for p in patch[1:]: | ||
p._internal_update(kwargs) | ||
p.set_label('_nolegend_') | ||
if nx == 1: | ||
return tops[0], bins, patches[0] | ||
Uh oh!
There was an error while loading.Please reload this page.