MCPcopy
hub / github.com/pydata/xarray / label_from_attrs

Function label_from_attrs

xarray/plot/utils.py:533–557  ·  view source on GitHub ↗

Makes informative labels if variable metadata (attrs) follows CF conventions.

(da: DataArray | None, extra: str = "")

Source from the content-addressed store, hash-verified

531
532
533def label_from_attrs(da: DataArray | None, extra: str = "") -> str:
534 """Makes informative labels if variable metadata (attrs) follows
535 CF conventions."""
536 if da is None:
537 return ""
538
539 name: str = "{}"
540 if "long_name" in da.attrs:
541 name = name.format(da.attrs["long_name"])
542 elif "standard_name" in da.attrs:
543 name = name.format(da.attrs["standard_name"])
544 elif da.name is not None:
545 name = name.format(da.name)
546 else:
547 name = ""
548
549 units = _get_units_from_attrs(da)
550
551 # Treat `name` differently if it's a latex sequence
552 if name.startswith("$") and (name.count("$") % 2 == 0):
553 return "$\n$".join(
554 textwrap.wrap(name + extra + units, 60, break_long_words=False)
555 )
556 else:
557 return "\n".join(textwrap.wrap(name + extra + units, 30))
558
559
560def _interval_to_mid_points(array: Iterable[pd.Interval]) -> np.ndarray:

Callers 15

_infer_line_dataFunction · 0.90
lineFunction · 0.90
histFunction · 0.90
newplotfuncFunction · 0.90
_add_labelsFunction · 0.90
map_dataarrayMethod · 0.90
map_plot1dMethod · 0.90
map_dataarray_lineMethod · 0.90
add_legendMethod · 0.90
add_colorbarMethod · 0.90
set_axis_labelsMethod · 0.90
_set_labelsMethod · 0.90

Calls 6

_get_units_from_attrsFunction · 0.85
startswithMethod · 0.80
wrapMethod · 0.80
formatMethod · 0.45
countMethod · 0.45
joinMethod · 0.45

Tested by 1

test_label_from_attrsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…