Power scales

detroit.scale_pow() ScalePow
detroit.scale_pow(range_vals: list[float]) ScalePow
detroit.scale_pow(domain: list[int | float], range_vals: list[float]) ScalePow

Constructs a new pow scale with the specified domain and range, the exponent 1, the default interpolator and clamping disabled.

Parameters:
  • domain (list[Number]) – Array of numbers

  • range_vals (list[float]) – Array of values

Returns:

Scale object

Return type:

ScalePow

Examples

>>> scale = d3.scale_pow([0, 10], [0, 960])
>>> steps = 10
>>> for x in range(steps + 1):
...     print(x, scale(x))
...
...
0 0.0
1 96.0
2 192.0
3 288.0
4 384.0
5 480.0
6 576.0
7 672.0
8 768.0
9 864.0
10 960.0

With exponent = 2:

>>> scale = scale.set_exponent(2)
>>> for x in range(steps + 1):
...     print(x, scale(x))
...
...
0 0.0
1 9.6
2 38.4
3 86.39999999999999
4 153.6
5 240.0
6 345.59999999999997
7 470.4
8 614.4
9 777.6
10 960.0
detroit.scale_sqrt() ScalePow
detroit.scale_sqrt(range_vals: list[float]) ScalePow
detroit.scale_sqrt(domain: list[int | float], range_vals: list[float]) ScalePow

Constructs a new pow scale with the specified domain and range, the exponent 0.5, the default interpolator and clamping disabled.

Parameters:
  • domain (list[Number]) – Array of numbers

  • range_vals (list[float]) – Array of values

Returns:

Scale object

Return type:

ScalePow

Examples

>>> scale = d3.scale_sqrt([0, 10], [0, 960])
>>> steps = 10
>>> for x in range(steps + 1):
...     print(x, scale(x))
...
...
0 0.0
1 303.5786553761644
2 429.3250516799596
3 525.8136552049594
4 607.1573107523288
5 678.8225099390855
6 743.612802471824
7 803.1936254727125
8 858.6501033599192
9 910.7359661284933
10 960.0
class detroit.scale.pow.ScalePow(t: ~collections.abc.Callable[[float], float] = <function identity>, u: ~collections.abc.Callable[[float], float] = <function identity>, exponent: float | int = 1)

Power (“pow”) scales are similar to linear scales, except an exponential transform is applied to the input domain value before the output range value is computed. Each range value y can be expressed as a function of the domain value x: \(y = m \cdot x^k + b\), where \(k\) is the exponent value. Power scales also support negative domain values, in which case the input value and the resulting output value are multiplied by -1.

Parameters:
  • t (Callable[[float], float]) – Transform function

  • u (Callable[[float], float]) – Untransform function

  • exponent (float | int) – Exponent

__call__(x: int | float | datetime) T

Given a value from the domain, returns the corresponding value from the range.

Parameters:

x (int | float) – Input value

Returns:

Corresponding value from the range

Return type:

T

invert(y: T) int | float

Given a value from the range, returns the corresponding value from the domain. Inversion is useful for interaction, say to determine the data value corresponding to the position of the mouse.

Parameters:

y (T) – Input value

Returns:

Corresponding value from the domain

Return type:

Number

set_domain(domain)

Sets the scale’s domain to the specified array of numbers.

Parameters:

domain (list[Number] | list[datetime]) – Domain

Returns:

Itself

Return type:

Transformer[T]

set_range(range_vals)

Sets the scale’s range to the specified array of values

Parameters:

range_vals (list[T]) – Range values

Returns:

Itself

Return type:

Transformer[T]

set_range_round(range_vals: list[T]) Transformer

Sets the scale’s range to the specified array of values and sets scale’s interpolator to interpolate_round.

Parameters:

range_vals (list[T]) – Range values

Returns:

Itself

Return type:

Transformer

set_clamp(clamp: bool) Transformer

Enables or disables clamping.

Parameters:

clamp (bool) – Clamp value

Returns:

Itself

Return type:

Transformer

set_interpolate(interpolate: Callable[[T, T], Callable[[float], T]]) Transformer[T]

Sets the scale’s range interpolator factory.

Parameters:

interpolate (Callable[[T, T], Callable[[float], T]]) – Interpolate function

Returns:

Itself

Return type:

Transformer[T]

set_unknown(unknown: Any) Transformer

Sets the output value of the scale for undefined or NaN input values.

Parameters:

unknown (Any) – Unknown value

Returns:

Itself

Return type:

Transformer

ticks(count: int | None = None) list[int | float]

Returns approximately count representative values from the scale’s domain.

Parameters:

count (int | None) – Count. If specified, the scale may return more or fewer values depending on the domain

Returns:

Tick values are uniformly spaced, have human-readable values (such as multiples of powers of 10), and are guaranteed to be within the extent of the domain. Ticks are often used to display reference lines, or tick marks, in conjunction with the visualized data.

Return type:

list[Number]

tick_format(count: int | None = None, specifier: str | None = None) Callable[[int | float], str]

Returns a number format function suitable for displaying a tick value, automatically computing the appropriate precision based on the fixed interval between tick values. The specified count should have the same value as the count that is used to generate the tick values.

Parameters:
  • count (int | None) – Count. It should have the same value as the count that is used to generate the tick values.

  • specifier (str | None) – Specifier

Returns:

Tick format function

Return type:

Callable[[Number], str]

nice(count: int | None = None) LinearBase

Extends the domain so that it starts and ends on nice round values.

Parameters:

count (int | None) – Count argument allows greater control over the step size used to extend the bounds, guaranteeing that the returned ticks will exactly cover the domain

Returns:

Itself

Return type:

LinearBase