MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / _convert_dx

Method _convert_dx

lib/matplotlib/axes/_axes.py:2207–2253  ·  view source on GitHub ↗

Small helper to do logic of width conversion flexibly. *dx* and *x0* have units, but *xconv* has already been converted to unitless (and is an ndarray). This allows the *dx* to have units that are different from *x0*, but are still accepted by the ``__add__

(dx, x0, xconv, convert)

Source from the content-addressed store, hash-verified

2205
2206 @staticmethod
2207 def _convert_dx(dx, x0, xconv, convert):
2208 """
2209 Small helper to do logic of width conversion flexibly.
2210
2211 *dx* and *x0* have units, but *xconv* has already been converted
2212 to unitless (and is an ndarray). This allows the *dx* to have units
2213 that are different from *x0*, but are still accepted by the
2214 ``__add__`` operator of *x0*.
2215 """
2216
2217 # x should be an array...
2218 assert type(xconv) is np.ndarray
2219
2220 if xconv.size == 0:
2221 # xconv has already been converted, but maybe empty...
2222 return convert(dx)
2223
2224 try:
2225 # attempt to add the width to x0; this works for
2226 # datetime+timedelta, for instance
2227
2228 # only use the first element of x and x0. This saves
2229 # having to be sure addition works across the whole
2230 # vector. This is particularly an issue if
2231 # x0 and dx are lists so x0 + dx just concatenates the lists.
2232 # We can't just cast x0 and dx to numpy arrays because that
2233 # removes the units from unit packages like `pint` that
2234 # wrap numpy arrays.
2235 try:
2236 x0 = cbook._safe_first_finite(x0)
2237 except (TypeError, IndexError, KeyError):
2238 pass
2239
2240 try:
2241 x = cbook._safe_first_finite(xconv)
2242 except (TypeError, IndexError, KeyError):
2243 x = xconv
2244
2245 if np.iterable(dx):
2246 dx = [convert(x0 + ddx) - x for ddx in dx]
2247 else:
2248 dx = convert(x0 + dx) - x
2249 except (ValueError, TypeError, AttributeError):
2250 # if the above fails (for any reason) just fallback to what
2251 # we do by default and convert dx by itself.
2252 dx = convert(dx)
2253 return dx
2254
2255 def _parse_bar_color_args(self, kwargs):
2256 """

Callers 2

barMethod · 0.95
broken_barhMethod · 0.95

Calls 1

convertFunction · 0.50

Tested by

no test coverage detected