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

Skip to content

Commit 495e76d

Browse files
committed
If stem markerfmt does not contain a color, use the color from linefmt
instead of the hard-coded 'C0'. While this is technically a breaking API change, it is unlikely that somebody wants to color their lines, but intentionally leaves their marker color as default 'C0'. I think there is no simple mechanism that'd allow a deprecation. All I can think of would require user actions at the beginning and at the end of the deprecation period. Given that this should only affect very few users and there is a simple workaround (state both formats explicitly).
1 parent c9bc943 commit 495e76d

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,6 +2821,8 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28212821
28222822
The *locs*-positions are optional. The formats may be provided either
28232823
as positional or as keyword-arguments.
2824+
Passing *markerfmt* and *basefmt* positionally is deprecated since
2825+
Matplotlib 3.5.
28242826
28252827
Parameters
28262828
----------
@@ -2853,8 +2855,8 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28532855
28542856
markerfmt : str, optional
28552857
A string defining the color and/or shape of the markers at the stem
2856-
heads. Default: 'C0o', i.e. filled circles with the first color of
2857-
the color cycle.
2858+
heads. Default: 'o', i.e. filled circles. If the color is not
2859+
given, the color from *linefmt* is used.
28582860
28592861
basefmt : str, default: 'C3-' ('C2-' in classic mode)
28602862
A format string defining the properties of the baseline.
@@ -2924,9 +2926,15 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
29242926
linestyle, linemarker, linecolor = _process_plot_format(linefmt)
29252927

29262928
if markerfmt is None:
2927-
markerfmt = args[1] if len(args) > 1 else "C0o"
2929+
markerfmt = args[1] if len(args) > 1 else "o"
29282930
markerstyle, markermarker, markercolor = \
29292931
_process_plot_format(markerfmt)
2932+
if markermarker is None:
2933+
markermarker = 'o'
2934+
if markercolor is None:
2935+
markercolor = linecolor
2936+
2937+
print(markerstyle, markermarker, markercolor)
29302938

29312939
if basefmt is None:
29322940
basefmt = (args[2] if len(args) > 2 else

lib/matplotlib/tests/test_axes.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3833,6 +3833,7 @@ def test_stem(use_line_collection):
38333833

38343834

38353835
def test_stem_args():
3836+
"""Test that stem() correctly identifies x and y values."""
38363837
def _assert_equal(stem_container, expected):
38373838
x, y = map(list, stem_container.markerline.get_data())
38383839
assert x == expected[0]
@@ -3853,6 +3854,64 @@ def _assert_equal(stem_container, expected):
38533854
_assert_equal(ax.stem(y, 'r--'), expected=([0, 1, 2], y))
38543855

38553856

3857+
def test_stem_markerfmt():
3858+
"""Test that stem(..., markerfmt=...) produces the intended markers."""
3859+
def _assert_equal(stem_container, linecolor=None, markercolor=None,
3860+
marker=None):
3861+
"""
3862+
Check that the given StemContainer has the properties listed as
3863+
keyword-arguments.
3864+
"""
3865+
if linecolor is not None:
3866+
assert mcolors.same_color(
3867+
stem_container.stemlines.get_color(),
3868+
linecolor)
3869+
if markercolor is not None:
3870+
assert mcolors.same_color(
3871+
stem_container.markerline.get_color(),
3872+
markercolor)
3873+
if marker is not None:
3874+
assert stem_container.markerline.get_marker() == marker
3875+
3876+
fig, ax = plt.subplots()
3877+
3878+
x = [1, 3, 5]
3879+
y = [9, 8, 7]
3880+
3881+
# no linefmt
3882+
_assert_equal(ax.stem(x, y), markercolor='C0')
3883+
_assert_equal(ax.stem(x, y, markerfmt='x'), markercolor='C0', marker='x')
3884+
_assert_equal(ax.stem(x, y, markerfmt='rx'), markercolor='r', marker='x')
3885+
3886+
# positional linefmt
3887+
_assert_equal(
3888+
ax.stem(x, y, 'r'), # marker color follows linefmt if not given
3889+
linecolor='r', markercolor='r', marker='o')
3890+
_assert_equal(
3891+
ax.stem(x, y, 'rx'), # the marker is currently not taken from linefmt
3892+
linecolor='r', markercolor='r', marker='o')
3893+
_assert_equal(
3894+
ax.stem(x, y, 'r', markerfmt='x'), # only marker type specified
3895+
linecolor='r', markercolor='r', marker='x')
3896+
_assert_equal(
3897+
ax.stem(x, y, 'r', markerfmt='g'), # only marker color specified
3898+
linecolor='r', markercolor='g', marker='o')
3899+
_assert_equal(
3900+
ax.stem(x, y, 'r', markerfmt='gx'), # marker type and color specified
3901+
linecolor='r', markercolor='g', marker='x')
3902+
3903+
# with linefmt kwarg
3904+
_assert_equal(
3905+
ax.stem(x, y, linefmt='r'),
3906+
linecolor='r', markercolor='r', marker='o')
3907+
_assert_equal(
3908+
ax.stem(x, y, linefmt='r', markerfmt='x'),
3909+
linecolor='r', markercolor='r', marker='x')
3910+
_assert_equal(
3911+
ax.stem(x, y, linefmt='r', markerfmt='gx'),
3912+
linecolor='r', markercolor='g', marker='x')
3913+
3914+
38563915
def test_stem_dates():
38573916
fig, ax = plt.subplots(1, 1)
38583917
xs = [dateutil.parser.parse("2013-9-28 11:00:00"),

0 commit comments

Comments
 (0)