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

Skip to content

Commit 6af8acd

Browse files
committed
Make plot handle 2-D x and/or y
svn path=/trunk/matplotlib/; revision=2914
1 parent 529fef2 commit 6af8acd

4 files changed

Lines changed: 102 additions & 55 deletions

File tree

API_CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Plot can handle 2-D x and/or y; it plots the columns.
2+
13
Added linewidth kwarg to bar and barh.
24

35
Made the default Artist._transform None (rather than invoking

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2006-12-09 Added support for plotting 2-D arrays with plot:
2+
columns are plotted as in Matlab - EF
3+
14
2006-12-09 Added linewidth kwarg to bar and barh; fixed arg
25
checking bugs - EF
36

lib/matplotlib/axes.py

Lines changed: 94 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
transpose, log, log10, Float, Float32, ravel, zeros,\
77
Int16, Int32, Int, Float64, ceil, indices, \
88
shape, which, where, sqrt, asum, compress, maximum, minimum, \
9-
typecode, concatenate, newaxis, reshape, resize
9+
typecode, concatenate, newaxis, reshape, resize, repeat
1010

1111
import numerix.ma as ma
1212

@@ -217,76 +217,112 @@ def set_patchprops(self, fill_poly, **kwargs):
217217
func = getattr(fill_poly,funcName)
218218
func(val)
219219

220+
def _xy_from_y(self, y):
221+
y = ma.asarray(y)
222+
if len(y.shape) == 1:
223+
y = y[:,newaxis]
224+
nr, nc = y.shape
225+
x = arange(nr)
226+
return x,y
227+
228+
def _xy_from_xy(self, x, y):
229+
x = ma.asarray(x)
230+
y = ma.asarray(y)
231+
if len(x.shape) == 1:
232+
x = x[:,newaxis]
233+
if len(y.shape) == 1:
234+
y = y[:,newaxis]
235+
nrx, ncx = x.shape
236+
nry, ncy = y.shape
237+
assert nrx == nry, 'Dimensions of x and y are incompatible'
238+
if ncx == ncy:
239+
return x, y
240+
if ncx == 1:
241+
x = repeat(x, ncy, axis=1)
242+
if ncy == 1:
243+
y = repeat(y, ncx, axis=1)
244+
assert x.shape == y.shape, 'Dimensions of x and y are incompatible'
245+
return x, y
246+
247+
220248
def _plot_1_arg(self, y, **kwargs):
221249
assert self.command == 'plot', 'fill needs at least 2 arguments'
222-
color = self._get_next_cycle_color()
223-
224-
assert(iterable(y))
225-
try: N=max(y.shape)
226-
except AttributeError: N = len(y)
227-
ret = Line2D(arange(N), y,
250+
ret = []
251+
x, y = self._xy_from_y(y)
252+
for j in range(y.shape[1]):
253+
color = self._get_next_cycle_color()
254+
seg = Line2D(x, y[:,j],
228255
color = color,
229256
)
230-
self.set_lineprops(ret, **kwargs)
257+
self.set_lineprops(seg, **kwargs)
258+
ret.append(seg)
231259
return ret
232260

233261
def _plot_2_args(self, tup2, **kwargs):
234262
if is_string_like(tup2[1]):
235263

236264
assert self.command == 'plot', 'fill needs at least 2 non-string arguments'
237265
y, fmt = tup2
238-
assert(iterable(y))
239266
linestyle, marker, color = _process_plot_format(fmt)
240-
if color is None:
241-
color = self._get_next_cycle_color()
242-
243-
try: N=max(y.shape)
244-
except AttributeError: N = len(y)
245-
246-
ret = Line2D(xdata=arange(N), ydata=y,
247-
color=color, linestyle=linestyle, marker=marker,
267+
ret = []
268+
x, y = self._xy_from_y(y)
269+
for j in range(y.shape[1]):
270+
_color = color
271+
if color is None:
272+
_color = self._get_next_cycle_color()
273+
seg = Line2D(x, y[:,j],
274+
color = _color,
275+
linestyle=linestyle, marker=marker,
248276
)
249-
self.set_lineprops(ret, **kwargs)
277+
self.set_lineprops(seg, **kwargs)
278+
ret.append(seg)
250279
return ret
251280
else:
252281

253-
x,y = tup2
254-
#print self.count, self.Ncolors, self.count % self.Ncolors
255-
assert(iterable(x))
256-
assert(iterable(y))
282+
x, y = self._xy_from_xy(*tup2)
257283
if self.command == 'plot':
258-
color = self._get_next_cycle_color()
259-
ret = Line2D(x, y,
284+
ret = []
285+
for j in range(y.shape[1]):
286+
color = self._get_next_cycle_color()
287+
seg = Line2D(x[:,j], y[:,j],
260288
color = color,
261289
)
262-
self.set_lineprops(ret, **kwargs)
290+
self.set_lineprops(seg, **kwargs)
291+
ret.append(seg)
263292
elif self.command == 'fill':
264-
ret = Polygon( zip(x,y), fill=True, )
265-
self.set_patchprops(ret, **kwargs)
266-
293+
ret = []
294+
for j in range(y.shape[1]):
295+
seg = Polygon( zip(x,y), fill=True, )
296+
self.set_patchprops(seg, **kwargs)
297+
ret.append(seg)
267298
return ret
268299

269300
def _plot_3_args(self, tup3, **kwargs):
301+
x, y = self._xy_from_xy(tup3[0], tup3[1])
270302
if self.command == 'plot':
271-
x, y, fmt = tup3
272-
assert(iterable(x))
273-
assert(iterable(y))
274-
303+
fmt = tup3[2]
275304
linestyle, marker, color = _process_plot_format(fmt)
276-
if color is None:
277-
color = self._get_next_cycle_color()
278-
279-
ret = Line2D(x, y,
280-
color=color, linestyle=linestyle, marker=marker,
281-
)
282-
self.set_lineprops(ret, **kwargs)
305+
ret = []
306+
for j in range(y.shape[1]):
307+
color_ = color
308+
if color is None:
309+
color_ = self._get_next_cycle_color()
310+
seg = Line2D(x[:,j], y[:,j],
311+
color=_color,
312+
linestyle=linestyle, marker=marker,
313+
)
314+
self.set_lineprops(seg, **kwargs)
315+
ret.append(seg)
283316
if self.command == 'fill':
284-
x, y, facecolor = tup3
285-
ret = Polygon(zip(x,y),
286-
facecolor = facecolor,
287-
fill=True,
288-
)
289-
self.set_patchprops(ret, **kwargs)
317+
facecolor = tup3[2]
318+
ret = []
319+
for j in range(y.shape[1]):
320+
seg = Polygon(zip(x[:j],y[:,j]),
321+
facecolor = facecolor,
322+
fill=True,
323+
)
324+
self.set_patchprops(seg, **kwargs)
325+
ret.append(seg)
290326
return ret
291327

292328
def _grab_next_args(self, *args, **kwargs):
@@ -296,27 +332,30 @@ def _grab_next_args(self, *args, **kwargs):
296332

297333
if len(remaining)==0: return
298334
if len(remaining)==1:
299-
yield self._plot_1_arg(remaining[0], **kwargs)
335+
for seg in self._plot_1_arg(remaining[0], **kwargs):
336+
yield seg
300337
remaining = []
301338
continue
302339
if len(remaining)==2:
303-
yield self._plot_2_args(remaining, **kwargs)
340+
for seg in self._plot_2_args(remaining, **kwargs):
341+
yield seg
304342
remaining = []
305343
continue
306344
if len(remaining)==3:
307345
if not is_string_like(remaining[2]):
308346
raise ValueError, 'third arg must be a format string'
309-
yield self._plot_3_args(remaining, **kwargs)
347+
for seg in self._plot_3_args(remaining, **kwargs):
348+
yield seg
310349
remaining=[]
311350
continue
312351
if is_string_like(remaining[2]):
313-
yield self._plot_3_args(remaining[:3], **kwargs)
352+
for seg in self._plot_3_args(remaining[:3], **kwargs):
353+
yield seg
314354
remaining=remaining[3:]
315355
else:
316-
yield self._plot_2_args(remaining[:2], **kwargs)
356+
for seg in self._plot_2_args(remaining[:2], **kwargs):
357+
yield seg
317358
remaining=remaining[2:]
318-
#yield self._plot_2_args(remaining[:2])
319-
#remaining=args[2:]
320359

321360
ValueType=type(zero())
322361
def makeValue(v):
@@ -2086,6 +2125,9 @@ def plot(self, *args, **kwargs):
20862125
plot(y) # plot y using x as index array 0..N-1
20872126
plot(y, 'r+') # ditto, but with red plusses
20882127
2128+
If x and/or y is 2-Dimensional, then the corresponding columns
2129+
will be plotted.
2130+
20892131
An arbitrary number of x, y, fmt groups can be specified, as in
20902132
20912133
a.plot(x1, y1, 'g^', x2, y2, 'g-')

lib/matplotlib/lines.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def set_color(self, color):
428428
"""
429429
Set the color of the line
430430
431-
ACCEPTS: any matplotlib color
431+
ACCEPTS: any matplotlib color
432432
"""
433433
self._color = color
434434

@@ -477,7 +477,7 @@ def set_markeredgecolor(self, ec):
477477
"""
478478
Set the marker edge color
479479
480-
ACCEPTS: any matplotlib color
480+
ACCEPTS: any matplotlib color
481481
"""
482482
self._markeredgecolor = ec
483483

@@ -493,7 +493,7 @@ def set_markerfacecolor(self, fc):
493493
"""
494494
Set the marker face color
495495
496-
ACCEPTS: any matplotlib color
496+
ACCEPTS: any matplotlib color
497497
"""
498498
self._markerfacecolor = fc
499499

0 commit comments

Comments
 (0)