|
168 | 168 | import logging
|
169 | 169 | import locale
|
170 | 170 | import math
|
| 171 | +from numbers import Integral |
171 | 172 |
|
172 | 173 | import numpy as np
|
173 | 174 |
|
@@ -584,11 +585,42 @@ def set_powerlimits(self, lims):
|
584 | 585 |
|
585 | 586 | def format_data_short(self, value):
|
586 | 587 | # docstring inherited
|
| 588 | + if isinstance(value, np.ma.MaskedArray) and value.mask: |
| 589 | + return "" |
| 590 | + if isinstance(value, Integral): |
| 591 | + fmt = "%d" |
| 592 | + else: |
| 593 | + if self.axis.__name__ in ["xaxis", "yaxis"]: |
| 594 | + if self.axis.__name__ == "xaxis": |
| 595 | + axis_trf = self.axis.axes.get_xaxis_transform() |
| 596 | + axis_inv_trf = axis_trf.inverted() |
| 597 | + screen_xy = axis_trf.transform((value, 0)) |
| 598 | + neighbor_values = axis_inv_trf.transform( |
| 599 | + screen_xy + [[-1, 0], [+1, 0]])[:, 0] |
| 600 | + else: # yaxis: |
| 601 | + axis_trf = self.axis.axes.get_yaxis_transform() |
| 602 | + axis_inv_trf = axis_trf.inverted() |
| 603 | + screen_xy = axis_trf.transform((0, value)) |
| 604 | + neighbor_values = axis_inv_trf.transform( |
| 605 | + screen_xy + [[0, -1], [0, +1]])[:, 1] |
| 606 | + delta = abs(neighbor_values - value).max() |
| 607 | + else: |
| 608 | + # Rough approximation: no more than 1e4 pixels. |
| 609 | + delta = self.axis.get_view_interval() / 1e4 |
| 610 | + # If e.g. value = 45.67 and delta = 0.02, then we want to round to |
| 611 | + # 2 digits after the decimal point (floor(log10(0.02)) = -2); |
| 612 | + # 45.67 contributes 2 digits before the decimal point |
| 613 | + # (floor(log10(45.67)) + 1 = 2): the total is 4 significant digits. |
| 614 | + # A value of 0 contributes 1 "digit" before the decimal point. |
| 615 | + sig_digits = max( |
| 616 | + 0, |
| 617 | + (math.floor(math.log10(abs(value))) + 1 if value else 1) |
| 618 | + - math.floor(math.log10(delta))) |
| 619 | + fmt = f"%-#.{sig_digits}g" |
587 | 620 | return (
|
588 |
| - "" if isinstance(value, np.ma.MaskedArray) and value.mask else |
589 | 621 | self.fix_minus(
|
590 |
| - locale.format_string("%-12g", (value,)) if self._useLocale else |
591 |
| - "%-12g" % value)) |
| 622 | + locale.format_string(fmt, (value,)) if self._useLocale else |
| 623 | + fmt % value)) |
592 | 624 |
|
593 | 625 | def format_data(self, value):
|
594 | 626 | # docstring inherited
|
|
0 commit comments