diff --git a/xray/conventions.py b/xray/conventions.py index 6fa4a77bbf2..3f608b8e374 100644 --- a/xray/conventions.py +++ b/xray/conventions.py @@ -825,7 +825,8 @@ def stackable(dim): var_coord_names = var_attrs['coordinates'].split() if all(k in variables for k in var_coord_names): coord_names.update(var_coord_names) - del var_attrs['coordinates'] + # del var_attrs['coordinates'] + var_attrs['coordinates'] = tuple(var_coord_names) if decode_coords and 'coordinates' in attributes: attributes = OrderedDict(attributes) @@ -943,9 +944,14 @@ def _encode_coordinates(variables, attributes, non_dim_coord_names): for var_name, coord_names in variable_coordinates.items(): attrs = variables[var_name].attrs if 'coordinates' in attrs: - raise ValueError('cannot serialize coordinates because variable ' - "%s already has an attribute 'coordinates'" - % var_name) + if (isinstance(attrs['coordinates'], tuple) and + len(attrs['coordinates']) == 2): + # Using CF coordinates + coord_names = attrs['coordinates'] + else: + raise ValueError('cannot serialize coordinates because variable ' + "%s already has an attribute 'coordinates'" + % var_name) attrs['coordinates'] = ' '.join(map(str, coord_names)) # These coordinates are not associated with any particular variables, so we diff --git a/xray/plot/plot.py b/xray/plot/plot.py index 2e229cd2f21..3f2d51b282e 100644 --- a/xray/plot/plot.py +++ b/xray/plot/plot.py @@ -62,20 +62,31 @@ def _infer_xy_labels(plotfunc, darray, x, y): '{1} or {2} for y' .format(y, *dims)) + cf_coords = getattr(darray, 'coordinates', None) + # Get label names if x and y: xlab = x ylab = y elif x and not y: xlab = x - del dims[dims.index(x)] - ylab = dims.pop() + if cf_coords: + ylab = cf_coords[1] + else: + del dims[dims.index(x)] + ylab = dims.pop() elif y and not x: ylab = y - del dims[dims.index(y)] - xlab = dims.pop() + if cf_coords: + xlab = cf_coords[0] + else: + del dims[dims.index(y)] + xlab = dims.pop() else: - ylab, xlab = dims + if cf_coords: + xlab, ylab = cf_coords + else: + ylab, xlab = dims return xlab, ylab