8000 Merge pull request #29735 from meeseeksmachine/auto-backport-of-pr-29… · matplotlib/matplotlib@519b3e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 519b3e5

Browse files
authored
Merge pull request #29735 from meeseeksmachine/auto-backport-of-pr-29719-on-v3.10.x
Backport PR #29719 on branch v3.10.x (Fix passing singleton sequence-type styles to hist)
2 parents 01d66d1 + 6bc61bb commit 519b3e5

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
@@ -7273,15 +7273,26 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
72737273
labels = [] if label is None else np.atleast_1d(np.asarray(label, str))
72747274

72757275
if histtype == "step":
7276-
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get('edgecolor',
7277-
colors)))
7276+
ec = kwargs.get('edgecolor', colors)
72787277
else:
7279-
edgecolors = itertools.cycle(np.atleast_1d(kwargs.get("edgecolor", None)))
7278+
ec = kwargs.get('edgecolor', None)
7279+
if ec is None or cbook._str_lower_equal(ec, 'none'):
7280+
edgecolors = itertools.repeat(ec)
7281+
else:
7282+
edgecolors = itertools.cycle(mcolors.to_rgba_array(ec))
7283+
7284+
fc = kwargs.get('facecolor', colors)
7285+
if cbook._str_lower_equal(fc, 'none'):
7286+
facecolors = itertools.repeat(fc)
7287+
else:
7288+
facecolors = itertools.cycle(mcolors.to_rgba_array(fc))
72807289

7281-
facecolors = itertools.cycle(np.atleast_1d(kwargs.get('facecolor', colors)))
72827290
hatches = itertools.cycle(np.atleast_1d(kwargs.get('hatch', None)))
72837291
linewidths = itertools.cycle(np.atleast_1d(kwargs.get('linewidth', None)))
7284-
linestyles = itertools.cycle(np.atleast_1d(kwargs.get('linestyle', None)))
7292+
if 'linestyle' in kwargs:
7293+
linestyles = itertools.cycle(mlines._get_dash_patterns(kwargs['linestyle']))
7294+
else:
7295+
linestyles = itertools.repeat(None)
72857296

72867297
for patch, lbl in itertools.zip_longest(patches, labels):
72877298
if not patch:

lib/matplotlib/collections.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -626,17 +626,8 @@ def set_linestyle(self, ls):
626626
':', '', (offset, on-off-seq)}. See `.Line2D.set_linestyle` for a
627627
complete description.
628628
"""
629-
try:
630-
dashes = [mlines._get_dash_pattern(ls)]
631-
except ValueError:
632-
try:
633-
dashes = [mlines._get_dash_pattern(x) for x in ls]
634-
except ValueError as err:
635-
emsg = f'Do not know how to convert {ls!r} to dashes'
636-
raise ValueError(emsg) from err
637-
638629
# get the list of raw 'unscaled' dash patterns
639-
self._us_linestyles = dashes
630+
self._us_linestyles = mlines._get_dash_patterns(ls)
640631

641632
# broadcast and scale the lw and dash patterns
642633
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
@@ -4762,6 +4762,27 @@ def test_hist_vectorized_params(fig_test, fig_ref, kwargs):
47624762
zorder=(len(xs)-i)/2)
47634763

47644764

4765+
def test_hist_sequence_type_styles():
4766+
facecolor = ('r', 0.5)
4767+
edgecolor = [0.5, 0.5, 0.5]
4768+
linestyle = (0, (1, 1))
4769+
4770+
arr = np.random.uniform(size=50)
4771+
_, _, bars = plt.hist(arr, facecolor=facecolor, edgecolor=edgecolor,
4772+
linestyle=linestyle)
4773+
assert mcolors.same_color(bars[0].get_facecolor(), facecolor)
4774+
assert mcolors.same_color(bars[0].get_edgecolor(), edgecolor)
4775+
assert bars[0].get_linestyle() == linestyle
4776+
4777+
4778+
def test_hist_color_none():
4779+
arr = np.random.uniform(size=50)
4780+
# No edgecolor is the default but check that it can be explicitly passed.
4781+
_, _, bars = plt.hist(arr, facecolor='none', edgecolor='none')
4782+
assert bars[0].get_facecolor(), (0, 0, 0, 0)
4783+
assert bars[0].get_edgecolor(), (0, 0, 0, 0)
4784+
4785+
47654786
@pytest.mark.parametrize('kwargs, patch_face, patch_edge',
47664787
# 'C0'(blue) stands for the first color of the
47674788
# default color cycle as well as the patch.facecolor rcParam

0 commit comments

Comments
 (0)
0