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

Skip to content

Commit 9cfa335

Browse files
committed
Simplify plot argument handling code.
The only side effect of the change should be that previously, plotting multiple columns required that x and y have the same number of columns, or that one of them have only one column. Now the plotting simply cycles through the columns of both, wrapping back to the first column as needed. svn path=/trunk/matplotlib/; revision=7298
1 parent fffd84a commit 9cfa335

File tree

2 files changed

+77
-180
lines changed

2 files changed

+77
-180
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2009-07-27 Simplify argument handling code for plot method. -EF
2+
13
2009-07-25 Allow "plot(1, 2, 'r*')" to work. - EF
24

35
2009-07-22 Added an 'interp' keyword to griddata so the faster linear

lib/matplotlib/axes.py

Lines changed: 75 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,6 @@ def set_patchprops(self, fill_poly, **kwargs):
199199
func = getattr(fill_poly,funcName)
200200
func(val)
201201

202-
def _xy_from_y(self, y):
203-
if self.axes.yaxis is not None:
204-
b = self.axes.yaxis.update_units(y)
205-
if b: return np.arange(len(y)), y, False
206-
207-
if not ma.isMaskedArray(y):
208-
y = np.asarray(y)
209-
if len(y.shape) == 1:
210-
y = y[:,np.newaxis]
211-
nr, nc = y.shape
212-
x = np.arange(nr)
213-
if len(x.shape) == 1:
214-
x = x[:,np.newaxis]
215-
return x,y, True
216-
217202
def _xy_from_xy(self, x, y):
218203
if self.axes.xaxis is not None and self.axes.yaxis is not None:
219204
bx = self.axes.xaxis.update_units(x)
@@ -223,195 +208,105 @@ def _xy_from_xy(self, x, y):
223208
if by:
224209
y = self.axes.convert_yunits(y)
225210

226-
x = ma.asarray(np.atleast_1d(x))
227-
y = ma.asarray(np.atleast_1d(y))
211+
x = np.atleast_1d(x) #like asanyarray, but converts scalar to array
212+
y = np.atleast_1d(y)
213+
if x.shape[0] != y.shape[0]:
214+
raise ValueError("x and y must have same first dimension")
215+
if x.ndim > 2 or y.ndim > 2:
216+
raise ValueError("x and y can be no greater than 2-D")
217+
228218
if x.ndim == 1:
229219
x = x[:,np.newaxis]
230220
if y.ndim == 1:
231221
y = y[:,np.newaxis]
232-
nrx, ncx = x.shape
233-
nry, ncy = y.shape
234-
assert nrx == nry, 'Dimensions of x and y are incompatible'
235-
if ncx == ncy:
236-
return x, y, True
237-
if ncx == 1:
238-
x = np.repeat(x, ncy, axis=1)
239-
if ncy == 1:
240-
y = np.repeat(y, ncx, axis=1)
241-
assert x.shape == y.shape, 'Dimensions of x and y are incompatible'
242-
return x, y, True
243-
244-
245-
def _plot_1_arg(self, y, **kwargs):
246-
assert self.command == 'plot', 'fill needs at least 2 arguments'
222+
return x, y
223+
224+
def _makeline(self, x, y, kw, kwargs):
225+
kw = kw.copy() # Don't modify the original kw.
226+
if not 'color' in kw:
227+
kw['color'] = self._get_next_cycle_color()
228+
# (can't use setdefault because it always evaluates
229+
# its second argument)
230+
seg = mlines.Line2D(x, y,
231+
axes=self.axes,
232+
**kw
233+
)
234+
self.set_lineprops(seg, **kwargs)
235+
return seg
236+
237+
def _makefill(self, x, y, kw, kwargs):
238+
try:
239+
facecolor = kw['color']
240+
except KeyError:
241+
facecolor = self._get_next_cycle_color()
242+
seg = mpatches.Polygon(np.hstack(
243+
(x[:,np.newaxis],y[:,np.newaxis])),
244+
facecolor = facecolor,
245+
fill=True,
246+
closed=kw['closed']
247+
)
248+
self.set_patchprops(seg, **kwargs)
249+
return seg
250+
251+
252+
def _plot_args(self, tup, kwargs):
247253
ret = []
248-
249-
x, y, multicol = self._xy_from_y(y)
250-
251-
if multicol:
252-
for j in xrange(y.shape[1]):
253-
color = self._get_next_cycle_color()
254-
seg = mlines.Line2D(x, y[:,j],
255-
color = color,
256-
axes=self.axes,
257-
)
258-
self.set_lineprops(seg, **kwargs)
259-
ret.append(seg)
254+
if len(tup) > 1 and is_string_like(tup[-1]):
255+
linestyle, marker, color = _process_plot_format(tup[-1])
256+
tup = tup[:-1]
257+
elif len(tup) == 3:
258+
raise ValueError, 'third arg must be a format string'
260259
else:
261-
color = self._get_next_cycle_color()
262-
seg = mlines.Line2D(x, y,
263-
color = color,
264-
axes=self.axes,
265-
)
266-
self.set_lineprops(seg, **kwargs)
267-
ret.append(seg)
268-
269-
return ret
260+
linestyle, marker, color = None, None, None
261+
kw = {}
262+
for k, v in zip(('linestyle', 'marker', 'color'),
263+
(linestyle, marker, color)):
264+
if v is not None:
265+
kw[k] = v
270266

271-
def _plot_2_args(self, tup2, **kwargs):
272-
ret = []
273-
if is_string_like(tup2[1]):
274-
275-
assert self.command == 'plot', ('fill needs at least 2 non-string '
276-
'arguments')
277-
y, fmt = tup2
278-
x, y, multicol = self._xy_from_y(y)
279-
280-
linestyle, marker, color = _process_plot_format(fmt)
281-
282-
def makeline(x, y):
283-
_color = color
284-
if _color is None:
285-
_color = self._get_next_cycle_color()
286-
seg = mlines.Line2D(x, y,
287-
color=_color,
288-
linestyle=linestyle, marker=marker,
289-
axes=self.axes,
290-
)
291-
self.set_lineprops(seg, **kwargs)
292-
ret.append(seg)
293-
294-
if multicol:
295-
for j in xrange(y.shape[1]):
296-
makeline(x[:,j], y[:,j])
297-
else:
298-
makeline(x, y)
267+
y = np.atleast_1d(tup[-1])
299268

300-
return ret
269+
if len(tup) == 2:
270+
x = np.atleast_1d(tup[0])
301271
else:
272+
x = np.arange(y.shape[0], dtype=float)
302273

303-
x, y = tup2
304-
x, y, multicol = self._xy_from_xy(x, y)
305-
306-
def makeline(x, y):
307-
color = self._get_next_cycle_color()
308-
seg = mlines.Line2D(x, y,
309-
color=color,
310-
axes=self.axes,
311-
)
312-
self.set_lineprops(seg, **kwargs)
313-
ret.append(seg)
314-
315-
def makefill(x, y):
316-
facecolor = self._get_next_cycle_color()
317-
seg = mpatches.Polygon(np.hstack(
318-
(x[:,np.newaxis],y[:,np.newaxis])),
319-
facecolor = facecolor,
320-
fill=True,
321-
closed=closed
322-
)
323-
self.set_patchprops(seg, **kwargs)
324-
ret.append(seg)
325-
326-
if self.command == 'plot':
327-
func = makeline
328-
else:
329-
closed = kwargs.get('closed', True)
330-
func = makefill
331-
if multicol:
332-
for j in xrange(y.shape[1]):
333-
func(x[:,j], y[:,j])
334-
else:
335-
func(x, y)
336-
337-
338-
return ret
339-
340-
def _plot_3_args(self, tup3, **kwargs):
341-
ret = []
342-
343-
x, y, fmt = tup3
344-
x, y, multicol = self._xy_from_xy(x, y)
345-
346-
linestyle, marker, color = _process_plot_format(fmt)
347-
348-
def makeline(x, y):
349-
_color = color
350-
if _color is None:
351-
_color = self._get_next_cycle_color()
352-
seg = mlines.Line2D(x, y,
353-
color=_color,
354-
linestyle=linestyle, marker=marker,
355-
axes=self.axes,
356-
)
357-
self.set_lineprops(seg, **kwargs)
358-
ret.append(seg)
359-
360-
def makefill(x, y):
361-
facecolor = color
362-
seg = mpatches.Polygon(np.hstack(
363-
(x[:,np.newaxis],y[:,np.newaxis])),
364-
facecolor = facecolor,
365-
fill=True,
366-
closed=closed
367-
)
368-
self.set_patchprops(seg, **kwargs)
369-
ret.append(seg)
274+
x, y = self._xy_from_xy(x, y)
370275

371276
if self.command == 'plot':
372-
func = makeline
277+
func = self._makeline
373278
else:
374-
closed = kwargs.get('closed', True)
375-
func = makefill
279+
kw['closed'] = kwargs.get('closed', True)
280+
func = self._makefill
376281

377-
if multicol:
378-
for j in xrange(y.shape[1]):
379-
func(x[:,j], y[:,j])
380-
else:
381-
func(x, y)
282+
ncx, ncy = x.shape[1], y.shape[1]
283+
for j in xrange(max(ncx, ncy)):
284+
seg = func(x[:,j%ncx], y[:,j%ncy], kw, kwargs)
285+
ret.append(seg)
382286
return ret
383287

288+
384289
def _grab_next_args(self, *args, **kwargs):
385290

386291
remaining = args
387292
while 1:
388293

389-
if len(remaining)==0: return
390-
if len(remaining)==1:
391-
for seg in self._plot_1_arg(remaining[0], **kwargs):
392-
yield seg
393-
remaining = []
394-
continue
395-
if len(remaining)==2:
396-
for seg in self._plot_2_args(remaining, **kwargs):
397-
yield seg
398-
remaining = []
399-
continue
400-
if len(remaining)==3:
401-
if not is_string_like(remaining[2]):
402-
raise ValueError, 'third arg must be a format string'
403-
for seg in self._plot_3_args(remaining, **kwargs):
294+
if len(remaining)==0:
295+
return
296+
if len(remaining) <= 3:
297+
for seg in self._plot_args(remaining, kwargs):
404298
yield seg
405-
remaining=[]
406-
continue
299+
return
300+
407301
if is_string_like(remaining[2]):
408-
for seg in self._plot_3_args(remaining[:3], **kwargs):
409-
yield seg
410-
remaining=remaining[3:]
302+
isplit = 3
411303
else:
412-
for seg in self._plot_2_args(remaining[:2], **kwargs):
413-
yield seg
414-
remaining=remaining[2:]
304+
isplit = 2
305+
306+
for seg in self._plot_args(remaining[:isplit], kwargs):
307+
yield seg
308+
remaining=remaining[isplit:]
309+
415310

416311

417312
class Axes(martist.Artist):

0 commit comments

Comments
 (0)