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

Skip to content

Commit 78b881e

Browse files
committed
Fix passing stem markerfmt positionally when locs are not given
the signature is stem([locs], heads, linefmt=None, ...) So we should support both: ``` stem(heads, linefmt='r') stem(heads, 'r') ``` We had a kwonly deprecation for 3.5 that was aiming at `stem([locs], heads, *, linefmt=None, ...)` but now I'd rather relax this to `stem([locs], heads, linefmt=None, *, ...)` because it's reasonable to still support `stem(heads, 'r')`. That's analogous to `plot(y, 'r')`. The code overhead for supporting both positional and keyword passing for the single `linefmt` parameter is bearable.
1 parent f278686 commit 78b881e

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Passing *linefmt* positionally is undeprecated
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Positional use of all formatting parameters in `.stem` has been deprecated
5+
since Matplotlib 3.5. This deprecation is relaxed so that one can still pass
6+
*linefmt* positionally, i.e. ``stem(x, y, 'r')``.

lib/matplotlib/axes/_axes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,12 +2900,15 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
29002900
heads, = args
29012901
locs = np.arange(len(heads))
29022902
args = ()
2903+
elif isinstance(args[1], str):
2904+
heads, *args = args
2905+
locs = np.arange(len(heads))
29032906
else:
29042907
locs, heads, *args = args
2905-
if args:
2908+
if len(args) > 1:
29062909
_api.warn_deprecated(
29072910
"3.5",
2908-
message="Passing the linefmt parameter positionally is "
2911+
message="Passing the markerfmt parameter positionally is "
29092912
"deprecated since Matplotlib %(since)s; the "
29102913
"parameter will become keyword-only %(removal)s.")
29112914

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,16 +3801,24 @@ def test_stem(use_line_collection):
38013801

38023802

38033803
def test_stem_args():
3804+
def _assert_equal(stem_container, expected):
3805+
x, y = map(list, stem_container.markerline.get_data())
3806+
assert x == expected[0]
3807+
assert y == expected[1]
3808+
38043809
fig, ax = plt.subplots()
38053810

3806-
x = list(range(10))
3807-
y = list(range(10))
3811+
x = [1, 3, 5]
3812+
y = [9, 8, 7]
38083813

38093814
# Test the call signatures
3810-
ax.stem(y)
3811-
ax.stem(x, y)
3812-
ax.stem(x, y, linefmt='r--')
3813-
ax.stem(x, y, linefmt='r--', basefmt='b--')
3815+
_assert_equal(ax.stem(y), expected=([0, 1, 2], y))
3816+
_assert_equal(ax.stem(x, y), expected=(x, y))
3817+
_assert_equal(ax.stem(x, y, linefmt='r--'), expected=(x, y))
3818+
_assert_equal(ax.stem(x, y, 'r--'), expected=(x, y))
3819+
_assert_equal(ax.stem(x, y, linefmt='r--', basefmt='b--'), expected=(x, y))
3820+
_assert_equal(ax.stem(y, linefmt='r--'), expected=([0, 1, 2], y))
3821+
_assert_equal(ax.stem(y, 'r--'), expected=([0, 1, 2], y))
38143822

38153823

38163824
def test_stem_dates():

0 commit comments

Comments
 (0)