Prepare data for usage with plt.scatter. Parameters ---------- darray : T_DataArray Base DataArray. coords_to_plot : MutableMapping[str, Hashable] Coords that will be plotted. plotfunc_name : str | None Name of the plotting function that will be used
(
darray: T_DataArray,
coords_to_plot: MutableMapping[str, Hashable],
plotfunc_name: str | None = None,
_is_facetgrid: bool = False,
)
| 146 | |
| 147 | |
| 148 | def _prepare_plot1d_data( |
| 149 | darray: T_DataArray, |
| 150 | coords_to_plot: MutableMapping[str, Hashable], |
| 151 | plotfunc_name: str | None = None, |
| 152 | _is_facetgrid: bool = False, |
| 153 | ) -> dict[str, T_DataArray]: |
| 154 | """ |
| 155 | Prepare data for usage with plt.scatter. |
| 156 | |
| 157 | Parameters |
| 158 | ---------- |
| 159 | darray : T_DataArray |
| 160 | Base DataArray. |
| 161 | coords_to_plot : MutableMapping[str, Hashable] |
| 162 | Coords that will be plotted. |
| 163 | plotfunc_name : str | None |
| 164 | Name of the plotting function that will be used. |
| 165 | |
| 166 | Returns |
| 167 | ------- |
| 168 | plts : dict[str, T_DataArray] |
| 169 | Dict of DataArrays that will be sent to matplotlib. |
| 170 | |
| 171 | Examples |
| 172 | -------- |
| 173 | >>> # Make sure int coords are plotted: |
| 174 | >>> a = xr.DataArray( |
| 175 | ... data=[1, 2], |
| 176 | ... coords={1: ("x", [0, 1], {"units": "s"})}, |
| 177 | ... dims=("x",), |
| 178 | ... name="a", |
| 179 | ... ) |
| 180 | >>> plts = xr.plot.dataarray_plot._prepare_plot1d_data( |
| 181 | ... a, coords_to_plot={"x": 1, "z": None, "hue": None, "size": None} |
| 182 | ... ) |
| 183 | >>> # Check which coords to plot: |
| 184 | >>> print({k: v.name for k, v in plts.items()}) |
| 185 | {'y': 'a', 'x': 1} |
| 186 | """ |
| 187 | # If there are more than 1 dimension in the array than stack all the |
| 188 | # dimensions so the plotter can plot anything: |
| 189 | if darray.ndim > 1: |
| 190 | # When stacking dims the lines will continue connecting. For floats |
| 191 | # this can be solved by adding a nan element in between the flattening |
| 192 | # points: |
| 193 | dims_T = [] |
| 194 | if np.issubdtype(darray.dtype, np.floating): |
| 195 | for v in ["z", "x"]: |
| 196 | dim = coords_to_plot.get(v, None) |
| 197 | if (dim is not None) and (dim in darray.dims): |
| 198 | darray_nan = np.nan * darray.isel({dim: -1}) |
| 199 | darray = concat( |
| 200 | [darray, darray_nan], |
| 201 | dim=dim, |
| 202 | coords="minimal", |
| 203 | compat="override", |
| 204 | join="exact", |
| 205 | ) |
no test coverage detected
searching dependent graphs…