Scatter plot with fixed markersize and hue no longer possible since v2022.10 ? #7268
Replies: 3 comments 2 replies
-
To make a guess which dimension to use is intentional, as there has been a precedent of guessing dimensions in other plotting methods: import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
temp = xr.DataArray(np.random.randn(10, 10, 2), coords=[np.arange(10), np.arange(10), [2021, 2022]], dims=["lon", "lat", "year"])
prec = xr.DataArray(np.random.randn(10, 10, 2), coords=[np.arange(10), np.arange(10), [2021, 2022]], dims=["lon", "lat", "year"])
one = xr.DataArray(np.random.randn(10, 10, 2), coords=[np.arange(10), np.arange(10), [2021, 2022]], dims=["lon", "lat", "year"])
blue = xr.DataArray(np.random.randn(10, 10, 2), coords=[np.arange(10), np.arange(10), [2021, 2022]], dims=["lon", "lat", "year"])
ds = xr.Dataset({"temperature": temp, "precipitation": prec, 1: one, "blue": blue})
# line adds hue:
ds.temperature.sel(lat=0).plot.line(x="lon")
# contourf adds hue:
ds.temperature.sel(year=2021).plot.contourf() And I believe that You can do one of these if information about dims is of no interest: # Stack the non interesting dims:
ds.stack(stacked_dim=[...]).plot.scatter(x="temperature", y="precipitation")
# Use matplotlib:
import matplotilib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(ds.temperature, ds.precipitation)
xarray/xarray/plot/dataarray_plot.py Line 154 in cc7e09a It's tricky to choose another default that can imply "Do not plot" however. Because xarray allows all hashables as a key. # 1 and "blue" is hashable and maybe someone wants to use that as an array name:
fig, ax = plt.subplots()
ds.plot.scatter(x="temperature", y="precipitation", hue="blue") |
Beta Was this translation helpful? Give feedback.
-
Yeah I ran in to this too. I think we should revert the auto-guessing bit.
This precedent only applies when its unambiguous. Otherwise we error. We should only ever make unambiguous choices, and force the user to be explicit about what they want otherwise |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
With v2022.09 and before, the scatter() plot method had the following behaviour:
So creating a simple scatter plot for all points (across dimensions) for the two variables.
With the latest xarray, we now get a different result for the same code:
So here xarray somehow decided to use the "year" dimension for markersize, and "lat" for color (hue) (I suppose starting from the last variable / dimension not used for x/y).
This is already a (breaking, but potentially intentional) change in behaviour, but I also don't seem to be able to get the previous behaviour to use a fixed color and markersize. It seems that both keywords only accept a dimension name, and not a constant value:
Also specifying eg
hue=None
to explicitly say "don't use a variable to control the hue" doesn't help.Looking at the whatsnew notes, this might be related to #6778? (didn't check though)
And assuming the change in behaviour was intentional, is it also on purpose there is no way to not use a variable markersizer or hue?
Beta Was this translation helpful? Give feedback.
All reactions