@@ -118,7 +118,7 @@ def __call__(self, ax, renderer):
118
118
self ._transform - ax .figure .transSubfigure )
119
119
120
120
121
- def _process_plot_format (fmt ):
121
+ def _process_plot_format (fmt , * , ambiguous_fmt_datakey = False ):
122
122
"""
123
123
Convert a MATLAB style color/line style format string to a (*linestyle*,
124
124
*marker*, *color*) tuple.
@@ -163,31 +163,31 @@ def _process_plot_format(fmt):
163
163
except ValueError :
164
164
pass # No, not just a color.
165
165
166
+ errfmt = ("{!r} is neither a data key nor a valid format string ({})"
167
+ if ambiguous_fmt_datakey else
168
+ "{!r} is not a valid format string ({})" )
169
+
166
170
i = 0
167
171
while i < len (fmt ):
168
172
c = fmt [i ]
169
173
if fmt [i :i + 2 ] in mlines .lineStyles : # First, the two-char styles.
170
174
if linestyle is not None :
171
- raise ValueError (
172
- f'Illegal format string { fmt !r} ; two linestyle symbols' )
175
+ raise ValueError (errfmt .format (fmt , "two linestyle symbols" ))
173
176
linestyle = fmt [i :i + 2 ]
174
177
i += 2
175
178
elif c in mlines .lineStyles :
176
179
if linestyle is not None :
177
- raise ValueError (
178
- f'Illegal format string { fmt !r} ; two linestyle symbols' )
180
+ raise ValueError (errfmt .format (fmt , "two linestyle symbols" ))
179
181
linestyle = c
180
182
i += 1
181
183
elif c in mlines .lineMarkers :
182
184
if marker is not None :
183
- raise ValueError (
184
- f'Illegal format string { fmt !r} ; two marker symbols' )
185
+ raise ValueError (errfmt .format (fmt , "two marker symbols" ))
185
186
marker = c
186
187
i += 1
187
188
elif c in mcolors .get_named_colors_mapping ():
188
189
if color is not None :
189
- raise ValueError (
190
- f'Illegal format string { fmt !r} ; two color symbols' )
190
+ raise ValueError (errfmt .format (fmt , "two color symbols" ))
191
191
color = c
192
192
i += 1
193
193
elif c == 'C' and i < len (fmt ) - 1 :
@@ -196,7 +196,7 @@ def _process_plot_format(fmt):
196
196
i += 2
197
197
else :
198
198
raise ValueError (
199
- f'Unrecognized character { c } in format string { fmt !r} ' )
199
+ errfmt . format ( fmt , f"unrecognized character { c !r} " ) )
200
200
201
201
if linestyle is None and marker is None :
202
202
linestyle = mpl .rcParams ['lines.linestyle' ]
@@ -293,6 +293,7 @@ def __call__(self, *args, data=None, **kwargs):
293
293
kwargs ["label" ] = mpl ._label_from_arg (
294
294
replaced [label_namer_idx ], args [label_namer_idx ])
295
295
args = replaced
296
+ ambiguous_fmt_datakey = data is not None and len (args ) == 2
296
297
297
298
if len (args ) >= 4 and not cbook .is_scalar_or_string (
298
299
kwargs .get ("label" )):
@@ -308,7 +309,8 @@ def __call__(self, *args, data=None, **kwargs):
308
309
if args and isinstance (args [0 ], str ):
309
310
this += args [0 ],
310
311
args = args [1 :]
311
- yield from self ._plot_args (this , kwargs )
312
+ yield from self ._plot_args (
313
+ this , kwargs , ambiguous_fmt_datakey = ambiguous_fmt_datakey )
312
314
313
315
def get_next_color (self ):
314
316
"""Return the next color in the cycle."""
@@ -402,7 +404,8 @@ def _makefill(self, x, y, kw, kwargs):
402
404
seg .set (** kwargs )
403
405
return seg , kwargs
404
406
405
- def _plot_args (self , tup , kwargs , return_kwargs = False ):
407
+ def _plot_args (self , tup , kwargs , * ,
408
+ return_kwargs = False , ambiguous_fmt_datakey = False ):
406
409
"""
407
410
Process the arguments of ``plot([x], y, [fmt], **kwargs)`` calls.
408
411
@@ -429,9 +432,13 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
429
432
The keyword arguments passed to ``plot()``.
430
433
431
434
return_kwargs : bool
432
- If true, return the effective keyword arguments after label
435
+ Whether to also return the effective keyword arguments after label
433
436
unpacking as well.
434
437
438
+ ambiguous_fmt_datakey : bool
439
+ Whether the format string in *tup* could also have been a
440
+ misspelled data key.
441
+
435
442
Returns
436
443
-------
437
444
result
@@ -445,7 +452,8 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
445
452
if len (tup ) > 1 and isinstance (tup [- 1 ], str ):
446
453
# xy is tup with fmt stripped (could still be (y,) only)
447
454
* xy , fmt = tup
448
- linestyle , marker , color = _process_plot_format (fmt )
455
+ linestyle , marker , color = _process_plot_format (
456
+ fmt , ambiguous_fmt_datakey = ambiguous_fmt_datakey )
449
457
elif len (tup ) == 3 :
450
458
raise ValueError ('third arg must be a format string' )
451
459
else :
0 commit comments