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

Skip to content

Commit f02a3f3

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 f02a3f3

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 18 additions & 4 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.
@@ -2918,15 +2920,27 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
29182920
else: # horizontal
29192921
heads, locs = self._process_unit_info([("x", heads), ("y", locs)])
29202922

2921-
# defaults for formats
2923+
# resolve line format
29222924
if linefmt is None:
29232925
linefmt = args[0] if len(args) > 0 else "C0-"
29242926
linestyle, linemarker, linecolor = _process_plot_format(linefmt)
29252927

2928+
# resolve marker format
29262929
if markerfmt is None:
2927-
markerfmt = args[1] if len(args) > 1 else "C0o"
2930+
# if not given as kwarg, check for positional or fall back to 'o'
2931+
markerfmt = args[1] if len(args) > 1 else "o"
2932+
if markerfmt == '':
2933+
markerfmt = ' ' # = empty line style; '' would resolve rcParams
29282934
markerstyle, markermarker, markercolor = \
29292935
_process_plot_format(markerfmt)
2936+
if markermarker is None:
2937+
markermarker = 'o'
2938+
if markerstyle is None:
2939+
markerstyle = 'None'
2940+
if markercolor is None:
2941+
markercolor = linecolor
2942+
2943+
print(markerstyle, markermarker, markercolor)
29302944

29312945
if basefmt is None:
29322946
basefmt = (args[2] if len(args) > 2 else

lib/matplotlib/tests/test_axes.py

Lines changed: 66 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,71 @@ 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+
assert stem_container.markerline.get_linestyle() == 'None'
3876+
3877+
fig, ax = plt.subplots()
3878+
3879+
x = [1, 3, 5]
3880+
y = [9, 8, 7]
3881+
3882+
# no linefmt
3883+
_assert_equal(ax.stem(x, y), markercolor='C0')
3884+
_assert_equal(ax.stem(x, y, markerfmt='x'), markercolor='C0', marker='x')
3885+
_assert_equal(ax.stem(x, y, markerfmt='rx'), markercolor='r', marker='x')
3886+
3887+
# positional linefmt
3888+
_assert_equal(
3889+
ax.stem(x, y, 'r'), # marker color follows linefmt if not given
3890+
linecolor='r', markercolor='r', marker='o')
3891+
_assert_equal(
3892+
ax.stem(x, y, 'rx'), # the marker is currently not taken from linefmt
3893+
linecolor='r', markercolor='r', marker='o')
3894+
_assert_equal(
3895+
ax.stem(x, y, 'r', markerfmt='x'), # only marker type specified
3896+
linecolor='r', markercolor='r', marker='x')
3897+
_assert_equal(
3898+
ax.stem(x, y, 'r', markerfmt='g'), # only marker color specified
3899+
linecolor='r', markercolor='g', marker='o')
3900+
_assert_equal(
3901+
ax.stem(x, y, 'r', markerfmt='gx'), # marker type and color specified
3902+
linecolor='r', markercolor='g', marker='x')
3903+
_assert_equal(
3904+
ax.stem(x, y, 'r', markerfmt=' '), # markerfmt=' ' for no marker
3905+
linecolor='r', markercolor='r', marker='None')
3906+
_assert_equal(
3907+
ax.stem(x, y, 'r', markerfmt=''), # markerfmt='' for no marker
3908+
linecolor='r', markercolor='r', marker='None')
3909+
3910+
# with linefmt kwarg
3911+
_assert_equal(
3912+
ax.stem(x, y, linefmt='r'),
3913+
linecolor='r', markercolor='r', marker='o')
3914+
_assert_equal(
3915+
ax.stem(x, y, linefmt='r', markerfmt='x'),
3916+
linecolor='r', markercolor='r', marker='x')
3917+
_assert_equal(
3918+
ax.stem(x, y, linefmt='r', markerfmt='gx'),
3919+
linecolor='r', markercolor='g', marker='x')
3920+
3921+
38563922
def test_stem_dates():
38573923
fig, ax = plt.subplots(1, 1)
38583924
xs = [dateutil.parser.parse("2013-9-28 11:00:00"),

0 commit comments

Comments
 (0)