From 5e7fc9f1fd5acf8bf9ce46568ed0d290de0f54d4 Mon Sep 17 00:00:00 2001 From: tomchor Date: Sat, 7 Aug 2021 07:13:47 -0700 Subject: [PATCH 01/11] try to wrap texts differently if they're latex code --- xarray/plot/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index f2f296096a5..72a756c3fdf 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -490,7 +490,10 @@ def _get_units_from_attrs(da): else: units = _get_units_from_attrs(da) - return "\n".join(textwrap.wrap(name + extra + units, 30)) + if name.startswith("$") and name.count("$")%2==0: + return "$\n$".join(textwrap.wrap(name + extra + units, 60)) + else: + return "\n".join(textwrap.wrap(name + extra + units, 30)) def _interval_to_mid_points(array): From e9be7c9f9f013cd94bbd095cb31cb672521702a8 Mon Sep 17 00:00:00 2001 From: tomchor Date: Sat, 7 Aug 2021 07:19:31 -0700 Subject: [PATCH 02/11] added space around modulo operator --- xarray/plot/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 72a756c3fdf..26b357ef670 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -490,7 +490,7 @@ def _get_units_from_attrs(da): else: units = _get_units_from_attrs(da) - if name.startswith("$") and name.count("$")%2==0: + if name.startswith("$") and (name.count("$") % 2 == 0): return "$\n$".join(textwrap.wrap(name + extra + units, 60)) else: return "\n".join(textwrap.wrap(name + extra + units, 30)) From ff1cbd26e248c5e116f2e89cddd8253aaf8c78c5 Mon Sep 17 00:00:00 2001 From: tomchor Date: Tue, 10 Aug 2021 08:24:13 -0700 Subject: [PATCH 03/11] prevents breaking up of long words --- xarray/plot/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 26b357ef670..6632a850e93 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -490,8 +490,9 @@ def _get_units_from_attrs(da): else: units = _get_units_from_attrs(da) + # 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)) + return "$\n$".join(textwrap.wrap(name + extra + units, 60, break_long_words=False)) else: return "\n".join(textwrap.wrap(name + extra + units, 30)) From 7b9fd9d7cd0abfe18fa04311abe7ac781059d0f9 Mon Sep 17 00:00:00 2001 From: tomchor Date: Wed, 11 Aug 2021 14:12:07 -0700 Subject: [PATCH 04/11] added test for label_from_attrs() function with a long latex string --- xarray/plot/utils.py | 3 ++- xarray/tests/test_utils.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 6632a850e93..b7562c4b36e 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -492,7 +492,8 @@ def _get_units_from_attrs(da): # 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)) + return "$\n$".join(textwrap.wrap(name + extra + units, 60, + break_long_words=False)) else: return "\n".join(textwrap.wrap(name + extra + units, 30)) diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 9c78caea4d6..3d0dccbd9a1 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -9,6 +9,7 @@ from xarray.core import duck_array_ops, utils from xarray.core.indexes import PandasIndex from xarray.core.utils import either_dict_or_kwargs, iterate_nested +from xarray.plot.utils import label_from_attrs from . import assert_array_equal, requires_cftime, requires_dask from .test_coding_times import _all_cftime_date_types @@ -42,6 +43,14 @@ def test_safe_cast_to_index(): assert expected.dtype == actual.dtype +def test_latex_name_isnt_split(): + da = xr.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 + + + @pytest.mark.parametrize( "a, b, expected", [["a", "b", np.array(["a", "b"])], [1, 2, pd.Index([1, 2])]] ) From fddccd0dd928b7fcd82dba34d50b2f27e070b6ba Mon Sep 17 00:00:00 2001 From: Tomas Chor Date: Sat, 14 Aug 2021 07:48:11 -0700 Subject: [PATCH 05/11] Update xarray/plot/utils.py Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> --- xarray/plot/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index b7562c4b36e..38900da6023 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -492,8 +492,9 @@ def _get_units_from_attrs(da): # 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)) + return "$\n$".join(textwrap.wrap( + name + extra + units, 60, break_long_words=False) + ) else: return "\n".join(textwrap.wrap(name + extra + units, 30)) From 14966c4aa0b2d446ab5952000d265c01800ea9f3 Mon Sep 17 00:00:00 2001 From: Tomas Chor Date: Sat, 14 Aug 2021 07:48:32 -0700 Subject: [PATCH 06/11] Update xarray/tests/test_utils.py Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> --- xarray/tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 3d0dccbd9a1..565c67bb077 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -46,7 +46,7 @@ def test_safe_cast_to_index(): def test_latex_name_isnt_split(): da = xr.DataArray() long_latex_name = r"$Ra_s = \mathrm{mean}(\epsilon_k) / \mu M^2_\infty$" - da.attrs = dict(long_name = long_latex_name) + da.attrs = dict(long_name=long_latex_name) assert label_from_attrs(da) == long_latex_name From 52959fb1606c99e7994156008881ea96660d1175 Mon Sep 17 00:00:00 2001 From: tomchor Date: Sat, 14 Aug 2021 07:51:29 -0700 Subject: [PATCH 07/11] moved test_latex_name_isnt_split() to test_plots --- xarray/tests/test_plot.py | 9 +++++++++ xarray/tests/test_utils.py | 8 -------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index ee8bafb8fa7..8ccba2d6a09 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -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, ) + + + +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 + diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 565c67bb077..b896dce6724 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -9,7 +9,6 @@ from xarray.core import duck_array_ops, utils from xarray.core.indexes import PandasIndex from xarray.core.utils import either_dict_or_kwargs, iterate_nested -from xarray.plot.utils import label_from_attrs from . import assert_array_equal, requires_cftime, requires_dask from .test_coding_times import _all_cftime_date_types @@ -43,13 +42,6 @@ def test_safe_cast_to_index(): assert expected.dtype == actual.dtype -def test_latex_name_isnt_split(): - da = xr.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 - - @pytest.mark.parametrize( "a, b, expected", [["a", "b", np.array(["a", "b"])], [1, 2, pd.Index([1, 2])]] From ff3937e5b3cb653eeefa6a6a98ea9b1a92e494a3 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Sat, 14 Aug 2021 11:17:11 -0600 Subject: [PATCH 08/11] Apply suggestions from code review --- xarray/tests/test_plot.py | 1 - xarray/tests/test_utils.py | 1 - 2 files changed, 2 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 8ccba2d6a09..4ac7e8bedf5 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2952,7 +2952,6 @@ def test_datarray_scatter(x, y, z, hue, markersize, row, col, add_legend, add_co ) - def test_latex_name_isnt_split(): da = xarray.DataArray() long_latex_name = r"$Ra_s = \mathrm{mean}(\epsilon_k) / \mu M^2_\infty$" diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index b896dce6724..9c78caea4d6 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -42,7 +42,6 @@ def test_safe_cast_to_index(): assert expected.dtype == actual.dtype - @pytest.mark.parametrize( "a, b, expected", [["a", "b", np.array(["a", "b"])], [1, 2, pd.Index([1, 2])]] ) From d35ecc8908825f991cac924a4ef6912208c82f32 Mon Sep 17 00:00:00 2001 From: tomchor Date: Sun, 15 Aug 2021 10:21:36 -0700 Subject: [PATCH 09/11] fixed test_latex_name_isnt_split() --- xarray/tests/test_plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 4ac7e8bedf5..2b31a7dd2cf 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2953,7 +2953,7 @@ def test_datarray_scatter(x, y, z, hue, markersize, row, col, add_legend, add_co def test_latex_name_isnt_split(): - da = xarray.DataArray() + da = xr.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 From 1567f0daacc426cdca2e0b310d910b7961470dc1 Mon Sep 17 00:00:00 2001 From: tomchor Date: Mon, 16 Aug 2021 18:11:24 -0700 Subject: [PATCH 10/11] ran code through pre-commit --- xarray/plot/utils.py | 4 ++-- xarray/tests/test_plot.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 38900da6023..af5859c1f14 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -492,8 +492,8 @@ def _get_units_from_attrs(da): # 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) + return "$\n$".join( + textwrap.wrap(name + extra + units, 60, break_long_words=False) ) else: return "\n".join(textwrap.wrap(name + extra + units, 30)) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 2b31a7dd2cf..e358bd47485 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2955,6 +2955,5 @@ def test_datarray_scatter(x, y, z, hue, markersize, row, col, add_legend, add_co def test_latex_name_isnt_split(): da = xr.DataArray() long_latex_name = r"$Ra_s = \mathrm{mean}(\epsilon_k) / \mu M^2_\infty$" - da.attrs = dict(long_name = long_latex_name) + da.attrs = dict(long_name=long_latex_name) assert label_from_attrs(da) == long_latex_name - From 80b4f8921b306c15e1e007342e35588deba5487b Mon Sep 17 00:00:00 2001 From: tomchor Date: Mon, 16 Aug 2021 18:17:53 -0700 Subject: [PATCH 11/11] updated whats-new.rst --- doc/whats-new.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index cef070c4936..3f43e0647ff 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -22,7 +22,8 @@ v0.19.1 (unreleased) New Features ~~~~~~~~~~~~ - +- Xarray now does a better job rendering variable names that are long LaTeX sequences when plotting (:issue:`5681`, :pull:`5682`). + By `Tomas Chor `_. Breaking changes ~~~~~~~~~~~~~~~~