Skip to content

Commit 01b27b0

Browse files
committed
Fix passing singleton sequence-type styles to hist
1 parent 70d0bf7 commit 01b27b0

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7193,15 +7193,26 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
71937193
labels = [] if label is None else np.atleast_1d(np.asarray(label, str))
71947194

71957195
if histtype == "step":
7196-
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor',
7197-
colors)))
7196+
ec = kwargs.get('edgecolor', colors)
71987197
else:
7199-
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get("edgecolor", None)))
7198+
ec = kwargs.get('edgecolor', None)
7199+
if ec is None or cbook._str_lower_equal(ec, 'none'):
7200+
edgecolors = itertools.repeat(ec)
7201+
else:
7202+
edgecolors = itertools.cycle(mcolors.to_rgba_array(ec))
7203+
7204+
fc = kwargs.get('facecolor', colors)
7205+
if cbook._str_lower_equal(fc, 'none'):
7206+
facecolors = itertools.repeat(fc)
7207+
else:
7208+
facecolors = itertools.cycle(mcolors.to_rgba_array(fc))
72007209

7201-
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor', colors)))
72027210
hatches = itertools.cycle(np.atleast_1d(kwargs.get('hatch', None)))
72037211
linewidths = itertools.cycle(np.atleast_1d(kwargs.get('linewidth', None)))
7204-
linestyles = itertools.cycle(np.atleast_1d(kwargs.get('linestyle', None)))
7212+
if 'linestyle' in kwargs:
7213+
linestyles = itertools.cycle(mlines._get_dash_patterns(kwargs['linestyle']))
7214+
else:
7215+
linestyles = itertools.repeat(None)
72057216

72067217
for patch, lbl in itertools.zip_longest(patches, labels):
72077218
if not patch:

lib/matplotlib/collections.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,8 @@ def set_linestyle(self, ls):
632632
':', '', (offset, on-off-seq)}. See `.Line2D.set_linestyle` for a
633633
complete description.
634634
"""
635-
try:
636-
dashes = [mlines._get_dash_pattern(ls)]
637-
except ValueError:
638-
try:
639-
dashes = [mlines._get_dash_pattern(x) for x in ls]
640-
except ValueError as err:
641-
emsg = f'Do not know how to convert {ls!r} to dashes'
642-
raise ValueError(emsg) from err
643-
644635
# get the list of raw 'unscaled' dash patterns
645-
self._us_linestyles = dashes
636+
self._us_linestyles = mlines._get_dash_patterns(ls)
646637

647638
# broadcast and scale the lw and dash patterns
648639
self._linewidths, self._linestyles = self._bcast_lwls(

lib/matplotlib/lines.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ def _get_dash_pattern(style):
6060
return offset, dashes
6161

6262

63+
def _get_dash_patterns(styles):
64+
"""Convert linestyle or sequence of linestyles to list of dash patterns."""
65+
try:
66+
patterns = [_get_dash_pattern(styles)]
67+
except ValueError:
68+
try:
69+
patterns = [_get_dash_pattern(x) for x in styles]
70+
except ValueError as err:
71+
emsg = f'Do not know how to convert {styles!r} to dashes'
72+
raise ValueError(emsg) from err
73+
74+
return patterns
75+
76+
6377
def _get_inverse_dash_pattern(offset, dashes):
6478
"""Return the inverse of the given dash pattern, for filling the gaps."""
6579
# Define the inverse pattern by moving the last gap to the start of the

lib/matplotlib/tests/test_axes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4831,6 +4831,27 @@ def test_hist_vectorized_params(fig_test, fig_ref, kwargs):
48314831
zorder=(len(xs)-i)/2)
48324832

48334833

4834+
def test_hist_sequence_type_styles():
4835+
facecolor = ('r', 0.5)
4836+
edgecolor = [0.5, 0.5, 0.5]
4837+
linestyle = (0, (1, 1))
4838+
4839+
arr = np.random.uniform(size=50)
4840+
_, _, bars = plt.hist(arr, facecolor=facecolor, edgecolor=edgecolor,
4841+
linestyle=linestyle)
4842+
assert mcolors.same_color(bars[0].get_facecolor(), facecolor)
4843+
assert mcolors.same_color(bars[0].get_edgecolor(), edgecolor)
4844+
assert bars[0].get_linestyle() == linestyle
4845+
4846+
4847+
def test_hist_color_none():
4848+
arr = np.random.uniform(size=50)
4849+
# No edgecolor is the default but check that it can be explicitly passed.
4850+
_, _, bars = plt.hist(arr, facecolor='none', edgecolor='none')
4851+
assert bars[0].get_facecolor(), (0, 0, 0, 0)
4852+
assert bars[0].get_edgecolor(), (0, 0, 0, 0)
4853+
4854+
48344855
@pytest.mark.parametrize('kwargs, patch_face, patch_edge',
48354856
# 'C0'(blue) stands for the first color of the
48364857
# default color cycle as well as the patch.facecolor rcParam

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy