Skip to content

Support Cn, n>9 in plot() shorthand format. #27943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/next_api_changes/behavior/27943-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
plot() shorthand format interprets "Cn" (n>9) as a color-cycle color
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously, ``plot(..., "-C11")`` would be interpreted as requesting a plot
using linestyle "-", color "C1" (color #1 of the color cycle), and marker "1"
("tri-down"). It is now interpreted as requesting linestyle "-" and color
"C11" (color #11 of the color cycle).

It is recommended to pass ambiguous markers (such as "1") explicitly using the
*marker* keyword argument. If the shorthand form is desired, such markers can
also be unambiguously set by putting them *before* the color string.
14 changes: 8 additions & 6 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
from numbers import Real
from operator import attrgetter
import re
import types

import numpy as np
Expand Down Expand Up @@ -189,13 +190,14 @@ def _process_plot_format(fmt, *, ambiguous_fmt_datakey=False):
raise ValueError(errfmt.format(fmt, "two color symbols"))
color = c
i += 1
elif c == 'C' and i < len(fmt) - 1:
color_cycle_number = int(fmt[i + 1])
color = mcolors.to_rgba(f"C{color_cycle_number}")
i += 2
elif c == "C":
cn_color = re.match(r"C\d+", fmt[i:])
if not cn_color:
raise ValueError(errfmt.format(fmt, "'C' must be followed by a number"))
color = mcolors.to_rgba(cn_color[0])
i += len(cn_color[0])
else:
raise ValueError(
errfmt.format(fmt, f"unrecognized character {c!r}"))
raise ValueError(errfmt.format(fmt, f"unrecognized character {c!r}"))

if linestyle is None and marker is None:
linestyle = mpl.rcParams['lines.linestyle']
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8511,6 +8511,8 @@ def test_empty_line_plots():
(":-", r"':-' is not a valid format string \(two linestyle symbols\)"),
("rk", r"'rk' is not a valid format string \(two color symbols\)"),
(":o-r", r"':o-r' is not a valid format string \(two linestyle symbols\)"),
("C", r"'C' is not a valid format string \('C' must be followed by a number\)"),
(".C", r"'.C' is not a valid format string \('C' must be followed by a number\)"),
))
@pytest.mark.parametrize("data", [None, {"string": range(3)}])
def test_plot_format_errors(fmt, match, data):
Expand Down Expand Up @@ -8543,6 +8545,11 @@ def test_plot_format():
line = ax.plot([1, 2, 3], 'k3')
assert line[0].get_marker() == '3'
assert line[0].get_color() == 'k'
fig, ax = plt.subplots()
line = ax.plot([1, 2, 3], '.C12:')
assert line[0].get_marker() == '.'
assert line[0].get_color() == mcolors.to_rgba('C12')
assert line[0].get_linestyle() == ':'


def test_automatic_legend():
Expand Down