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

Complex LaTeX expressions not rendering in long_name attribute with mixed text and math expressions in xarraypPlots #9764

Open
j-horak opened this issue Nov 11, 2024 · 2 comments
Labels
plan to close May be closeable, needs more eyeballs upstream issue

Comments

@j-horak
Copy link

j-horak commented Nov 11, 2024

What happened?

When I try to give a variable a long_name that's a complex latex expression prefixed with some data and then plot that variable the expression doesn't get rendered by latex. (Note that when the text is suffixed it does work).

image

What did you expect to happen?

Expected the name to get rendered properly.

image

Minimal Complete Verifiable Example

{python}
import numpy as np
from matplotlib import pyplot as plt
import xarray as xr
da = xr.DataArray(range(5), dims="x", coords = dict(x=range(5)))
name = r"x-data $Ra_s = \mathrm{mean}(\epsilon_k) / \mu M^2_\infty$"
da.x.attrs = dict(long_name = name)
da.plot()

plt.figure()
plt.plot(range(5))
plt.xlabel(name)


### MVCE confirmation

- [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- [X] Complete example — the example is self-contained, including all data and the text of any traceback.
- [X] Verifiable example — the example copy & pastes into an IPython prompt or [Binder notebook](https://mybinder.org/v2/gh/pydata/xarray/main?urlpath=lab/tree/doc/examples/blank_template.ipynb), returning the result.
- [X] New issue — a search of GitHub Issues suggests this is not a duplicate.
- [X] Recent environment — the issue occurs with the latest version of xarray and its dependencies.

### Relevant log output

_No response_

### Anything else we need to know?

Mixed math and text work if the long_name str starts with a math expression and its corresponding $ sign, e.g., `r"$a$ Data $Ra_s = \mathrm{mean}(\epsilon_k) / \mu M^2_\infty$"`.

Maybe this is related to #5681

### Environment

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.13.0 | packaged by conda-forge | (main, Oct  8 2024, 20:04:32) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 6.9.3-76060903-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: None

xarray: 2024.10.0
pandas: 2.2.3
numpy: 2.1.3
scipy: 1.14.1
netCDF4: None
pydap: None
h5netcdf: 1.4.0
h5py: 3.12.1
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.9.2
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.3.0
pip: None
conda: None
pytest: None
mypy: None
IPython: 8.29.0
sphinx: None

</details>
@j-horak j-horak added bug needs triage Issue that has not been reviewed by xarray team member labels Nov 11, 2024
Copy link

welcome bot commented Nov 11, 2024

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@dcherian dcherian added upstream issue plan to close May be closeable, needs more eyeballs and removed bug needs triage Issue that has not been reviewed by xarray team member labels Nov 11, 2024
@dcherian
Copy link
Contributor

We run a textwrap on the string because matplotlib won't do it for us:

textwrap.wrap(name + extra + units, 60, break_long_words=False)

It does not handle latex. It would be easy to skip wrapping if the first and last characters are $ I guess, anything else seems a bit too hard and complex. Honestly this is a matplotlib issue: https://discourse.matplotlib.org/t/auto-wrapping-text-within-a-plot-is-there-a-simpler-solution/14567 . We're just working around it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to close May be closeable, needs more eyeballs upstream issue
Projects
None yet
Development

No branches or pull requests

2 participants