Skip to content

Commit ec692d8

Browse files
anntzertacaswell
authored andcommitted
MNT: re-work how units are handled in axes._base
Reattach the unitful data to Line2D.
1 parent 9925db0 commit ec692d8

2 files changed

Lines changed: 42 additions & 34 deletions

File tree

‎lib/matplotlib/axes/_base.py‎

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,10 @@ def _xy_from_xy(self, x, y):
214214
if self.axes.xaxis is not None and self.axes.yaxis is not None:
215215
bx = self.axes.xaxis.update_units(x)
216216
by = self.axes.yaxis.update_units(y)
217-
218-
if self.command != 'plot':
219-
# the Line2D class can handle unitized data, with
220-
# support for post hoc unit changes etc. Other mpl
221-
# artists, e.g., Polygon which _process_plot_var_args
222-
# also serves on calls to fill, cannot. So this is a
223-
# hack to say: if you are not "plot", which is
224-
# creating Line2D, then convert the data now to
225-
# floats. If you are plot, pass the raw data through
226-
# to Line2D which will handle the conversion. So
227-
# polygons will not support post hoc conversions of
228-
# the unit type since they are not storing the orig
229-
# data. Hopefully we can rationalize this at a later
230-
# date - JDH
231-
if bx:
232-
x = self.axes.convert_xunits(x)
233-
if by:
234-
y = self.axes.convert_yunits(y)
217+
if bx:
218+
x = self.axes.convert_xunits(x)
219+
if by:
220+
y = self.axes.convert_yunits(y)
235221

236222
# like asanyarray, but converts scalar to array, and doesn't change
237223
# existing compatible sequences
@@ -374,26 +360,48 @@ def _plot_args(self, tup, kwargs):
374360
if 'label' not in kwargs or kwargs['label'] is None:
375361
kwargs['label'] = get_label(tup[-1], None)
376362

377-
if len(tup) == 2:
378-
x = _check_1d(tup[0])
379-
y = _check_1d(tup[-1])
363+
if len(tup) == 1:
364+
x, y = index_of(tup[0])
365+
elif len(tup) == 2:
366+
x, y = tup
380367
else:
381-
x, y = index_of(tup[-1])
382-
383-
x, y = self._xy_from_xy(x, y)
384-
385-
if self.command == 'plot':
386-
func = self._makeline
368+
assert False
369+
370+
deunitized_x, deunitized_y = self._xy_from_xy(x, y)
371+
# The previous call has registered the converters, if any, on the axes.
372+
# This check will need to be replaced by a comparison with the
373+
# DefaultConverter when that PR goes in.
374+
if self.axes.xaxis.converter is None or self.command is not "plot":
375+
xt, yt = deunitized_x.T, deunitized_y.T
387376
else:
388-
kw['closed'] = kwargs.get('closed', True)
389-
func = self._makefill
390-
391-
ncx, ncy = x.shape[1], y.shape[1]
377+
# np.asarray would destroy unit information so we need to construct
378+
# the 1D arrays to pass to Line2D.set_xdata manually... (but this
379+
# is only relevant if the command is "plot").
380+
381+
def to_list_of_lists(data):
382+
ndim = np.ndim(data)
383+
if ndim == 0:
384+
return [[data]]
385+
elif ndim == 1:
386+
return [data]
387+
elif ndim == 2:
388+
return zip(*data) # Transpose it.
389+
390+
xt, yt = map(to_list_of_lists, [x, y])
391+
392+
ncx, ncy = deunitized_x.shape[1], deunitized_y.shape[1]
392393
if ncx > 1 and ncy > 1 and ncx != ncy:
393394
cbook.warn_deprecated("2.2", "cycling among columns of inputs "
394395
"with non-matching shapes is deprecated.")
396+
395397
for j in xrange(max(ncx, ncy)):
396-
seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
398+
if self.command == "plot":
399+
seg = self._makeline(xt[j % ncx], yt[j % ncy], kw, kwargs)
400+
else:
401+
kw['closed'] = kwargs.get('closed', True)
402+
seg = self._makefill(deunitized_x[:, j % ncx],
403+
deunitized_y[:, j % ncy],
404+
kw, kwargs)
397405
ret.append(seg)
398406
return ret
399407

‎lib/matplotlib/cbook/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,8 +2312,8 @@ def index_of(y):
23122312
try:
23132313
return y.index.values, y.values
23142314
except AttributeError:
2315-
y = _check_1d(y)
2316-
return np.arange(y.shape[0], dtype=float), y
2315+
# Ensure that scalar y gives x == [0].
2316+
return np.arange((np.shape(y) or (1,))[0], dtype=float), y
23172317

23182318

23192319
def safe_first_element(obj):

0 commit comments

Comments
 (0)