What happened?
I was plotting some Datasets with fairly complicated labels and units, and I noticed a very strange behaviour when using the plot() method.
When both attrs["long_name"] and attrs["units"] contain \mathrm's or \text's, and at least one of them contains a \frac, and if the length of both strings passes a certain threshold, the label on the plot is not rendered as Latex anymore.
This is a sample DataArray plotted with xarray's plot() method:
And this is the same data plotted with matplotlib:
See code below to reproduce these images.
What did you expect to happen?
I expected the Latex label to be rendered correctly, as it is when I apply the label with matplotlib.
Minimal Complete Verifiable Example
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
xr.show_versions()
data = xr.DataArray(
np.random.randn(5, 10),
dims=("y", "x"),
coords={"x": np.linspace(0, 3, 10), "y": np.linspace(0, 20, 5)},
)
label = r"$\frac{\mathrm{x}}{\mathrm{A}}$"
units = r"$\mathrm{m~hello~very~long}$"
data["x"].attrs["long_name"] = label
data["x"].attrs["units"] = units
data.attrs["long_name"] = label
data.attrs["units"] = units
# with xarray
fig1 = plt.figure()
plt.title("with xarray .plot")
data.plot()
# with matplotlib
fig2 = plt.figure()
im = plt.pcolormesh(
data["x"].to_numpy(),
data["y"].to_numpy(),
data.to_numpy()[:-1, :-1],
)
cb = fig2.colorbar(im)
cb.set_label(label + f"[{units}]")
plt.title("with plt.pcolormesh")
plt.xlabel(label + f"[{units}]")
Steps to reproduce
Use MCVE above to produce two figures and compare the labels in each figure. Try making the labels shorter, or removing \frac or \mathrm from one of them.
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
Details
INSTALLED VERSIONS
commit: None
python: 3.13.14 | packaged by conda-forge | (main, Jun 12 2026, 09:44:24) [Clang 19.1.7 ]
python-bits: 64
OS: Darwin
OS-release: 25.5.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 2.0.0
libnetcdf: None
xarray: 2026.7.0
pandas: 3.0.3
numpy: 2.4.6
scipy: 1.18.0
netCDF4: None
pydap: None
h5netcdf: 1.8.1
h5py: 3.16.0
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: 2026.7.1
distributed: None
matplotlib: 3.11.0
cartopy: None
seaborn: 0.13.2
numbagg: 0.9.4
fsspec: 2026.6.0
cupy: None
pint: 0.25.3
sparse: None
flox: 0.11.2
numpy_groupies: 0.11.3
setuptools: 83.0.0
pip: 26.1.2
conda: None
pytest: 9.1.1
mypy: None
IPython: 8.39.0
sphinx: None
What happened?
I was plotting some Datasets with fairly complicated labels and units, and I noticed a very strange behaviour when using the
plot()method.When both
attrs["long_name"]andattrs["units"]contain\mathrm's or\text's, and at least one of them contains a\frac, and if the length of both strings passes a certain threshold, the label on the plot is not rendered as Latex anymore.This is a sample DataArray plotted with xarray's
plot()method:And this is the same data plotted with matplotlib:
See code below to reproduce these images.
What did you expect to happen?
I expected the Latex label to be rendered correctly, as it is when I apply the label with matplotlib.
Minimal Complete Verifiable Example
Steps to reproduce
Use MCVE above to produce two figures and compare the labels in each figure. Try making the labels shorter, or removing
\fracor\mathrmfrom one of them.MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
Details
INSTALLED VERSIONS
commit: None
python: 3.13.14 | packaged by conda-forge | (main, Jun 12 2026, 09:44:24) [Clang 19.1.7 ]
python-bits: 64
OS: Darwin
OS-release: 25.5.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 2.0.0
libnetcdf: None
xarray: 2026.7.0
pandas: 3.0.3
numpy: 2.4.6
scipy: 1.18.0
netCDF4: None
pydap: None
h5netcdf: 1.8.1
h5py: 3.16.0
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: 2026.7.1
distributed: None
matplotlib: 3.11.0
cartopy: None
seaborn: 0.13.2
numbagg: 0.9.4
fsspec: 2026.6.0
cupy: None
pint: 0.25.3
sparse: None
flox: 0.11.2
numpy_groupies: 0.11.3
setuptools: 83.0.0
pip: 26.1.2
conda: None
pytest: 9.1.1
mypy: None
IPython: 8.39.0
sphinx: None