Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves rendering of complex LaTeX expressions as long_names when plotting #5682

Merged
merged 12 commits into from
Aug 17, 2021
8 changes: 7 additions & 1 deletion xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,13 @@ def _get_units_from_attrs(da):
else:
units = _get_units_from_attrs(da)

return "\n".join(textwrap.wrap(name + extra + units, 30))
# Treat `name` differently if it's a latex sequence
if name.startswith("$") and (name.count("$") % 2 == 0):
return "$\n$".join(textwrap.wrap(
name + extra + units, 60, break_long_words=False)
)
else:
return "\n".join(textwrap.wrap(name + extra + units, 30))
dcherian marked this conversation as resolved.
Show resolved Hide resolved


def _interval_to_mid_points(array):
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2950,3 +2950,12 @@ def test_datarray_scatter(x, y, z, hue, markersize, row, col, add_legend, add_co
add_legend=add_legend,
add_colorbar=add_colorbar,
)



dcherian marked this conversation as resolved.
Show resolved Hide resolved
def test_latex_name_isnt_split():
da = xarray.DataArray()
long_latex_name = r"$Ra_s = \mathrm{mean}(\epsilon_k) / \mu M^2_\infty$"
da.attrs = dict(long_name = long_latex_name)
assert label_from_attrs(da) == long_latex_name

1 change: 1 addition & 0 deletions xarray/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_safe_cast_to_index():
assert expected.dtype == actual.dtype



dcherian marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize(
"a, b, expected", [["a", "b", np.array(["a", "b"])], [1, 2, pd.Index([1, 2])]]
)
Expand Down