Legend overlaps when visualizing a DataArray in xarray #5963
-
DescriptionWhen visualizing a DataArray using xarray, the legend overlaps and becomes cluttered. This issue does not occur when I use the same script in Visual Studio Code. Reproduce
Data: link Screencast: Screencast.from.2025-01-13.11-03-11.webmSystem InformationOperating System: Ubuntu 24.04.1 LTS Positron Version: Interpreter: 3.12.3 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @mehmetgoktug! Do you have a screenshot or video of the plot in Visual Studio Code? Are you expecting that a new plot is redrawn instead of the legend getting added to the existing plot? I was able to reproduce with the following: pip install matplotlib numpy xarray pooch # based on https://tutorial.xarray.dev/overview/xarray-in-45-min.html
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
xr.set_options(keep_attrs=True, display_expand_data=False)
np.set_printoptions(threshold=10, edgeitems=2)
# load the data set
ds = xr.tutorial.load_dataset("air_temperature")
lat = ds.air.lat.data # numpy array
lon = ds.air.lon.data # numpy array
temp = ds.air.data # numpy array
# plot the data
plt.pcolormesh(lon, lat, temp[0, :, :]);
# run this next line multiple times until the text starts overlapping
ds.air.isel(time=0).plot(x="lon"); CC @seeM for plot knowledge 😄 |
Beta Was this translation helpful? Give feedback.
I think this is expected behavior but it would be helpful to see the same in VSCode. Was that in a notebook or interactive window? And was it executed line-by-line?
Positron's plotting is intended to be interactive line-by-line. The various
plot()
methods here might be using a matplotlib function that adds a new legend without checking if a previous exists. You could consider opening an issue in the xarray repo to allow repeated executions.Or you can work around this by creating a new figure before each
plot()
call withplt.figure()
. That's what would happen behind the scenes in a Jupyter notebook.