Skip to content

Commit 83e20e4

Browse files
authored
Merge pull request matplotlib#23233 from timhoffm/stem-marker-color
FIX: Default stem marker color follows the linecolor
2 parents 8afcbc2 + 203cf26 commit 83e20e4

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
``stem(..., markerfmt=...)`` behavior
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The behavior of the *markerfmt* parameter of `~.Axes.stem` has changed:
4+
5+
- If *markerfmt* does not contain a color, the color is taken from *linefmt*.
6+
- If *markerfmt* does not contain a marker, the default is 'o'.
7+
8+
Before, *markerfmt* was passed unmodified to ``plot(..., fmt)``, which had
9+
a number of unintended side-effects; e.g. only giving a color switched to
10+
a solid line without markers.
11+
12+
For a simple call ``stem(x, y)`` without parameters, the new rules still
13+
reproduce the old behavior.

lib/matplotlib/axes/_axes.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,8 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28252825
28262826
The *locs*-positions are optional. The formats may be provided either
28272827
as positional or as keyword-arguments.
2828+
Passing *markerfmt* and *basefmt* positionally is deprecated since
2829+
Matplotlib 3.5.
28282830
28292831
Parameters
28302832
----------
@@ -2857,8 +2859,8 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28572859
28582860
markerfmt : str, optional
28592861
A string defining the color and/or shape of the markers at the stem
2860-
heads. Default: 'C0o', i.e. filled circles with the first color of
2861-
the color cycle.
2862+
heads. If the marker is not given, use the marker 'o', i.e. filled
2863+
circles. If the color is not given, use the color from *linefmt*.
28622864
28632865
basefmt : str, default: 'C3-' ('C2-' in classic mode)
28642866
A format string defining the properties of the baseline.
@@ -2923,16 +2925,27 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
29232925
else: # horizontal
29242926
heads, locs = self._process_unit_info([("x", heads), ("y", locs)])
29252927

2926-
# defaults for formats
2928+
# resolve line format
29272929
if linefmt is None:
29282930
linefmt = args[0] if len(args) > 0 else "C0-"
29292931
linestyle, linemarker, linecolor = _process_plot_format(linefmt)
29302932

2933+
# resolve marker format
29312934
if markerfmt is None:
2932-
markerfmt = args[1] if len(args) > 1 else "C0o"
2935+
# if not given as kwarg, check for positional or fall back to 'o'
2936+
markerfmt = args[1] if len(args) > 1 else "o"
2937+
if markerfmt == '':
2938+
markerfmt = ' ' # = empty line style; '' would resolve rcParams
29332939
markerstyle, markermarker, markercolor = \
29342940
_process_plot_format(markerfmt)
2935-
2941+
if markermarker is None:
2942+
markermarker = 'o'
2943+
if markerstyle is None:
2944+
markerstyle = 'None'
2945+
if markercolor is None:
2946+
markercolor = linecolor
2947+
2948+
# resolve baseline format
29362949
if basefmt is None:
29372950
basefmt = (args[2] if len(args) > 2 else
29382951
"C2-" if rcParams["_internal.classic_mode"] else "C3-")

lib/matplotlib/tests/test_axes.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,6 +3902,7 @@ def test_stem(use_line_collection):
39023902

39033903

39043904
def test_stem_args():
3905+
"""Test that stem() correctly identifies x and y values."""
39053906
def _assert_equal(stem_container, expected):
39063907
x, y = map(list, stem_container.markerline.get_data())
39073908
assert x == expected[0]
@@ -3922,6 +3923,71 @@ def _assert_equal(stem_container, expected):
39223923
_assert_equal(ax.stem(y, 'r--'), expected=([0, 1, 2], y))
39233924

39243925

3926+
def test_stem_markerfmt():
3927+
"""Test that stem(..., markerfmt=...) produces the intended markers."""
3928+
def _assert_equal(stem_container, linecolor=None, markercolor=None,
3929+
marker=None):
3930+
"""
3931+
Check that the given StemContainer has the properties listed as
3932+
keyword-arguments.
3933+
"""
3934+
if linecolor is not None:
3935+
assert mcolors.same_color(
3936+
stem_container.stemlines.get_color(),
3937+
linecolor)
3938+
if markercolor is not None:
3939+
assert mcolors.same_color(
3940+
stem_container.markerline.get_color(),
3941+
markercolor)
3942+
if marker is not None:
3943+
assert stem_container.markerline.get_marker() == marker
3944+
assert stem_container.markerline.get_linestyle() == 'None'
3945+
3946+
fig, ax = plt.subplots()
3947+
3948+
x = [1, 3, 5]
3949+
y = [9, 8, 7]
3950+
3951+
# no linefmt
3952+
_assert_equal(ax.stem(x, y), markercolor='C0', marker='o')
3953+
_assert_equal(ax.stem(x, y, markerfmt='x'), markercolor='C0', marker='x')
3954+
_assert_equal(ax.stem(x, y, markerfmt='rx'), markercolor='r', marker='x')
3955+
3956+
# positional linefmt
3957+
_assert_equal(
3958+
ax.stem(x, y, 'r'), # marker color follows linefmt if not given
3959+
linecolor='r', markercolor='r', marker='o')
3960+
_assert_equal(
3961+
ax.stem(x, y, 'rx'), # the marker is currently not taken from linefmt
3962+
linecolor='r', markercolor='r', marker='o')
3963+
_assert_equal(
3964+
ax.stem(x, y, 'r', markerfmt='x'), # only marker type specified
3965+
linecolor='r', markercolor='r', marker='x')
3966+
_assert_equal(
3967+
ax.stem(x, y, 'r', markerfmt='g'), # only marker color specified
3968+
linecolor='r', markercolor='g', marker='o')
3969+
_assert_equal(
3970+
ax.stem(x, y, 'r', markerfmt='gx'), # marker type and color specified
3971+
linecolor='r', markercolor='g', marker='x')
3972+
_assert_equal(
3973+
ax.stem(x, y, 'r', markerfmt=' '), # markerfmt=' ' for no marker
3974+
linecolor='r', markercolor='r', marker='None')
3975+
_assert_equal(
3976+
ax.stem(x, y, 'r', markerfmt=''), # markerfmt='' for no marker
3977+
linecolor='r', markercolor='r', marker='None')
3978+
3979+
# with linefmt kwarg
3980+
_assert_equal(
3981+
ax.stem(x, y, linefmt='r'),
3982+
linecolor='r', markercolor='r', marker='o')
3983+
_assert_equal(
3984+
ax.stem(x, y, linefmt='r', markerfmt='x'),
3985+
linecolor='r', markercolor='r', marker='x')
3986+
_assert_equal(
3987+
ax.stem(x, y, linefmt='r', markerfmt='gx'),
3988+
linecolor='r', markercolor='g', marker='x')
3989+
3990+
39253991
def test_stem_dates():
39263992
fig, ax = plt.subplots(1, 1)
39273993
xs = [dateutil.parser.parse("2013-9-28 11:00:00"),

0 commit comments

Comments
 (0)