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

Skip to content

Commit 753e53d

Browse files
committed
Simpler and stricter process_plot_format.
See changelog entry. The default values were removed from the docstring as they are not true, and depend not trivially on rcParams and effectively on the property cycle.
1 parent d72f069 commit 753e53d

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Stricter `~.Axes.plot` format string parsing
2+
````````````````````````````````````````````
3+
4+
In certain cases, `~.Axes.plot` would previously accept format strings
5+
specifying more than one linestyle (e.g. ``"---."`` which specifies both
6+
``"--"`` and ``"-."``); only use one of them would be used.
7+
8+
This now raises a ValueError instead.

lib/matplotlib/axes/_base.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434

3535
def _process_plot_format(fmt):
3636
"""
37-
Process a MATLAB style color/line style format string. Return a
38-
(*linestyle*, *color*) tuple as a result of the processing. Default
39-
values are ('-', 'b'). Example format strings include:
37+
Convert a MATLAB style color/line style format string to a (*linestyle*,
38+
*marker*, *color*) tuple.
39+
40+
Example format strings include:
4041
4142
* 'ko': black circles
4243
* '.b': blue dots
@@ -73,46 +74,40 @@ def _process_plot_format(fmt):
7374
except ValueError:
7475
pass # No, not just a color.
7576

76-
# handle the multi char special cases and strip them from the
77-
# string
78-
if fmt.find('--') >= 0:
79-
linestyle = '--'
80-
fmt = fmt.replace('--', '')
81-
if fmt.find('-.') >= 0:
82-
linestyle = '-.'
83-
fmt = fmt.replace('-.', '')
84-
if fmt.find(' ') >= 0:
85-
linestyle = 'None'
86-
fmt = fmt.replace(' ', '')
87-
88-
chars = [c for c in fmt]
89-
9077
i = 0
91-
while i < len(chars):
92-
c = chars[i]
93-
if c in mlines.lineStyles:
78+
while i < len(fmt):
79+
c = fmt[i]
80+
if fmt[i:i+2] in mlines.lineStyles: # First, the two-char styles.
81+
if linestyle is not None:
82+
raise ValueError(
83+
'Illegal format string "%s"; two linestyle symbols' % fmt)
84+
linestyle = fmt[i:i+2]
85+
i += 2
86+
elif c in mlines.lineStyles:
9487
if linestyle is not None:
9588
raise ValueError(
9689
'Illegal format string "%s"; two linestyle symbols' % fmt)
9790
linestyle = c
91+
i += 1
9892
elif c in mlines.lineMarkers:
9993
if marker is not None:
10094
raise ValueError(
10195
'Illegal format string "%s"; two marker symbols' % fmt)
10296
marker = c
97+
i += 1
10398
elif c in mcolors.get_named_colors_mapping():
10499
if color is not None:
105100
raise ValueError(
106101
'Illegal format string "%s"; two color symbols' % fmt)
107102
color = c
108-
elif c == 'C' and i < len(chars) - 1:
109-
color_cycle_number = int(chars[i + 1])
110-
color = mcolors.to_rgba("C{}".format(color_cycle_number))
111103
i += 1
104+
elif c == 'C' and i < len(fmt) - 1:
105+
color_cycle_number = int(fmt[i + 1])
106+
color = mcolors.to_rgba("C{}".format(color_cycle_number))
107+
i += 2
112108
else:
113109
raise ValueError(
114110
'Unrecognized character %c in format string' % c)
115-
i += 1
116111

117112
if linestyle is None and marker is None:
118113
linestyle = rcParams['lines.linestyle']

0 commit comments

Comments
 (0)