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

Method format_data

lib/matplotlib/ticker.py:1554–1604  ·  view source on GitHub ↗

Format a number in engineering notation, appending a letter representing the power of 1000 of the original number. Some examples: >>> format_data(0) # for self.places = 0 '0' >>> format_data(1000000) # for self.places = 1 '1.0 M'

(self, value)

Source from the content-addressed store, hash-verified

1552 return self.format_data(num)
1553
1554 def format_data(self, value):
1555 """
1556 Format a number in engineering notation, appending a letter
1557 representing the power of 1000 of the original number.
1558 Some examples:
1559
1560 >>> format_data(0) # for self.places = 0
1561 '0'
1562
1563 >>> format_data(1000000) # for self.places = 1
1564 '1.0 M'
1565
1566 >>> format_data(-1e-6) # for self.places = 2
1567 '-1.00 \N{MICRO SIGN}'
1568 """
1569 sign = 1
1570 fmt = "g" if self.places is None else f".{self.places:d}f"
1571
1572 if value < 0:
1573 sign = -1
1574 value = -value
1575
1576 if value != 0:
1577 pow10 = int(math.floor(math.log10(value) / 3) * 3)
1578 else:
1579 pow10 = 0
1580 # Force value to zero, to avoid inconsistencies like
1581 # format_eng(-0) = "0" and format_eng(0.0) = "0"
1582 # but format_eng(-0.0) = "-0.0"
1583 value = 0.0
1584
1585 pow10 = np.clip(pow10, min(self.ENG_PREFIXES), max(self.ENG_PREFIXES))
1586
1587 mant = sign * value / (10.0 ** pow10)
1588 # Taking care of the cases like 999.9..., which may be rounded to 1000
1589 # instead of 1 k. Beware of the corner case of values that are beyond
1590 # the range of SI prefixes (i.e. > 'Y').
1591 if (abs(float(format(mant, fmt))) >= 1000
1592 and pow10 < max(self.ENG_PREFIXES)):
1593 mant /= 1000
1594 pow10 += 3
1595
1596 unit_prefix = self.ENG_PREFIXES[int(pow10)]
1597 if self.unit or unit_prefix:
1598 suffix = f"{self.sep}{unit_prefix}{self.unit}"
1599 else:
1600 suffix = ""
1601 if self._usetex or self._useMathText:
1602 return f"${mant:{fmt}}${suffix}"
1603 else:
1604 return f"{mant:{fmt}}{suffix}"
1605
1606
1607class PercentFormatter(Formatter):

Callers 3

__call__Method · 0.95
get_offsetMethod · 0.95
format_engMethod · 0.95

Calls 1

clipMethod · 0.45

Tested by

no test coverage detected