'xarray:plot missing values

I am plotting some time series that are xarray object but I have data only for May-October. The result is this straight line:. Do you have any idea how to remove the dates from the x-axis that are not in the dataset? enter image description here



Solution 1:[1]

Since I see one block of contiguous data per year in your example, and xarray is not aware of the fact that you don't want to connect the points between the two years with lines, you could proceed like in the docs, here for an example dataset:

# create basic example dataset
da = xr.DataArray(
    np.linspace(0, 1826, num=1827),
    coords=[pd.date_range("1/1/2000", "31/12/2004", freq="D")],
    dims="time",
)

# the meat of the answer: making selection of data per year easier
groups = da.groupby("time.year")
# example usage of that
for year, group in groups:    # iterate over yearly blocks
    group.plot(label=year) 

You could also access data for 2000 via groups[2000].


For your example as per the comment:

groups = Anomalies.sel(lat=55, lon=12).groupby("time.year")
for year, group in groups:    # iterate over yearly blocks
    group.plot(label=year)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1