MCPcopy Index your code
hub / github.com/pydata/xarray / _determine_cmap_params

Function _determine_cmap_params

xarray/plot/utils.py:161–328  ·  view source on GitHub ↗

Use some heuristics to set good defaults for colorbar and range. Parameters ---------- plot_data : Numpy array Doesn't handle xarray objects Returns ------- cmap_params : dict Use depends on the type of the plotting function

(
    plot_data,
    vmin=None,
    vmax=None,
    cmap=None,
    center=None,
    robust=False,
    extend=None,
    levels=None,
    filled=True,
    norm=None,
    _is_facetgrid=False,
)

Source from the content-addressed store, hash-verified

159
160
161def _determine_cmap_params(
162 plot_data,
163 vmin=None,
164 vmax=None,
165 cmap=None,
166 center=None,
167 robust=False,
168 extend=None,
169 levels=None,
170 filled=True,
171 norm=None,
172 _is_facetgrid=False,
173):
174 """
175 Use some heuristics to set good defaults for colorbar and range.
176
177 Parameters
178 ----------
179 plot_data : Numpy array
180 Doesn't handle xarray objects
181
182 Returns
183 -------
184 cmap_params : dict
185 Use depends on the type of the plotting function
186 """
187 if TYPE_CHECKING:
188 import matplotlib as mpl
189 else:
190 mpl = attempt_import("matplotlib")
191
192 if plot_data.dtype.kind == "m":
193 unit, _ = np.datetime_data(plot_data.dtype)
194 zero = np.timedelta64(0, unit)
195 elif plot_data.dtype.kind == "M":
196 unit, _ = np.datetime_data(plot_data.dtype)
197 zero = np.datetime64(0, unit)
198 else:
199 zero = 0.0
200
201 if isinstance(levels, Iterable):
202 levels = sorted(levels)
203
204 calc_data = np.ravel(plot_data[np.isfinite(plot_data)])
205
206 # Handle all-NaN input data gracefully
207 if calc_data.size == 0:
208 # Arbitrary default for when all values are NaN
209 calc_data = np.array(zero)
210
211 # Setting center=False prevents a divergent cmap
212 possibly_divergent = center is not False
213
214 # Set center to 0 so math below makes sense but remember its state
215 center_is_none = False
216 if center is None:
217 center = zero
218 center_is_none = True

Callers 11

test_robustMethod · 0.90
test_centerMethod · 0.90
test_integer_levelsMethod · 0.90
test_list_levelsMethod · 0.90
test_divergentcontrolMethod · 0.90

Calls 7

attempt_importFunction · 0.90
is_scalarFunction · 0.90
_determine_extendFunction · 0.85
_build_discrete_cmapFunction · 0.85
linspaceMethod · 0.80
minMethod · 0.45
maxMethod · 0.45

Tested by 10

test_robustMethod · 0.72
test_centerMethod · 0.72
test_integer_levelsMethod · 0.72
test_list_levelsMethod · 0.72
test_divergentcontrolMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…