| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3412,10 +3412,15 @@ def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs): | |||
| 3412 | 3412 | """ | |
| 3413 | 3413 | Set the x-tick labels with list of string labels. | |
| 3414 | 3414 | ||
| 3415 | + .. warning:: | ||
| 3416 | + This method should only be used after fixing the tick positions | ||
| 3417 | + using `~.axes.Axes.set_xticks`. Otherwise, the labels may end up | ||
| 3418 | + in unexpected positions. | ||
| 3419 | + | ||
| 3415 | 3420 | Parameters | |
| 3416 | 3421 | ---------- | |
| 3417 | - labels : List[str] | ||
| 3418 | - List of string labels. | ||
| 3422 | + labels : list of str | ||
| 3423 | + The label texts. | ||
| 3419 | 3424 | ||
| 3420 | 3425 | fontdict : dict, optional | |
| 3421 | 3426 | A dictionary controlling the appearance of the ticklabels. | |
@@ -3426,12 +3431,13 @@ def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs): | |||
| 3426 | 3431 | 'verticalalignment': 'baseline', | |
| 3427 | 3432 | 'horizontalalignment': loc} | |
| 3428 | 3433 | ||
| 3429 | - minor : bool, optional | ||
| 3434 | + minor : bool, default: False | ||
| 3430 | 3435 | Whether to set the minor ticklabels rather than the major ones. | |
| 3431 | 3436 | ||
| 3432 | 3437 | Returns | |
| 3433 | 3438 | ------- | |
| 3434 | - A list of `~.text.Text` instances. | ||
| 3439 | + labels | ||
| 3440 | + A list of `~.text.Text` instances. | ||
| 3435 | 3441 | ||
| 3436 | 3442 | Other Parameters | |
| 3437 | 3443 | ----------------- | |
@@ -3792,12 +3798,17 @@ def get_yticklabels(self, minor=False, which=None): | |||
| 3792 | 3798 | ||
| 3793 | 3799 | def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs): | |
| 3794 | 3800 | """ | |
| 3795 | - Set the y-tick labels with list of strings labels. | ||
| 3801 | + Set the y-tick labels with list of string labels. | ||
| 3802 | + | ||
| 3803 | + .. warning:: | ||
| 3804 | + This method should only be used after fixing the tick positions | ||
| 3805 | + using `~.axes.Axes.set_yticks`. Otherwise, the labels may end up | ||
| 3806 | + in unexpected positions. | ||
| 3796 | 3807 | ||
| 3797 | 3808 | Parameters | |
| 3798 | 3809 | ---------- | |
| 3799 | - labels : List[str] | ||
| 3800 | - list of string labels | ||
| 3810 | + labels : list of str | ||
| 3811 | + The label texts. | ||
| 3801 | 3812 | ||
| 3802 | 3813 | fontdict : dict, optional | |
| 3803 | 3814 | A dictionary controlling the appearance of the ticklabels. | |
@@ -3808,12 +3819,13 @@ def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs): | |||
| 3808 | 3819 | 'verticalalignment': 'baseline', | |
| 3809 | 3820 | 'horizontalalignment': loc} | |
| 3810 | 3821 | ||
| 3811 | - minor : bool, optional | ||
| 3822 | + minor : bool, default: False | ||
| 3812 | 3823 | Whether to set the minor ticklabels rather than the major ones. | |
| 3813 | 3824 | ||
| 3814 | 3825 | Returns | |
| 3815 | 3826 | ------- | |
| 3816 | - A list of `~.text.Text` instances. | ||
| 3827 | + labels | ||
| 3828 | + A list of `~.text.Text` instances. | ||
| 3817 | 3829 | ||
| 3818 | 3830 | Other Parameters | |
| 3819 | 3831 | ---------------- | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | 5 | import datetime | |
| 6 | 6 | import logging | |
| 7 | + import warnings | ||
| 7 | 8 | ||
| 8 | 9 | import numpy as np | |
| 9 | 10 | ||
@@ -1632,6 +1633,10 @@ def set_major_formatter(self, formatter): | |||
| 1632 | 1633 | formatter : `~matplotlib.ticker.Formatter` | |
| 1633 | 1634 | """ | |
| 1634 | 1635 | cbook._check_isinstance(mticker.Formatter, formatter=formatter) | |
| 1636 | + if (isinstance(formatter, mticker.FixedFormatter) | ||
| 1637 | + and not isinstance(self.major.locator, mticker.FixedLocator)): | ||
| 1638 | + warnings.warn('FixedFormatter should only be used together with ' | ||
| 1639 | + 'FixedLocator', stacklevel=2) | ||
| 1635 | 1640 | self.isDefault_majfmt = False | |
| 1636 | 1641 | self.major.formatter = formatter | |
| 1637 | 1642 | formatter.set_axis(self) | |
@@ -1646,6 +1651,10 @@ def set_minor_formatter(self, formatter): | |||
| 1646 | 1651 | formatter : `~matplotlib.ticker.Formatter` | |
| 1647 | 1652 | """ | |
| 1648 | 1653 | cbook._check_isinstance(mticker.Formatter, formatter=formatter) | |
| 1654 | + if (isinstance(formatter, mticker.FixedFormatter) | ||
| 1655 | + and not isinstance(self.major.locator, mticker.FixedLocator)): | ||
| 1656 | + warnings.warn('FixedFormatter should only be used together with ' | ||
| 1657 | + 'FixedLocator', stacklevel=2) | ||
| 1649 | 1658 | self.isDefault_minfmt = False | |
| 1650 | 1659 | self.minor.formatter = formatter | |
| 1651 | 1660 | formatter.set_axis(self) | |
@@ -1697,6 +1706,11 @@ def set_ticklabels(self, ticklabels, *args, minor=False, **kwargs): | |||
| 1697 | 1706 | r""" | |
| 1698 | 1707 | Set the text values of the tick labels. | |
| 1699 | 1708 | ||
| 1709 | + .. warning:: | ||
| 1710 | + This method should only be used after fixing the tick positions | ||
| 1711 | + using `.Axis.set_ticks`. Otherwise, the labels may end up in | ||
| 1712 | + unexpected positions. | ||
| 1713 | + | ||
| 1700 | 1714 | Parameters | |
| 1701 | 1715 | ---------- | |
| 1702 | 1716 | ticklabels : sequence of str or of `Text`\s | |
@@ -1718,18 +1732,8 @@ def set_ticklabels(self, ticklabels, *args, minor=False, **kwargs): | |||
| 1718 | 1732 | "3.1", message="Additional positional arguments to " | |
| 1719 | 1733 | "set_ticklabels are ignored, and deprecated since Matplotlib " | |
| 1720 | 1734 | "3.1; passing them will raise a TypeError in Matplotlib 3.3.") | |
| 1721 | - get_labels = [] | ||
| 1722 | - for t in ticklabels: | ||
| 1723 | - # try calling get_text() to check whether it is Text object | ||
| 1724 | - # if it is Text, get label content | ||
| 1725 | - try: | ||
| 1726 | - get_labels.append(t.get_text()) | ||
| 1727 | - # otherwise add the label to the list directly | ||
| 1728 | - except AttributeError: | ||
| 1729 | - get_labels.append(t) | ||
| 1730 | - # replace the ticklabels list with the processed one | ||
| 1731 | - ticklabels = get_labels | ||
| 1732 | - | ||
| 1735 | + ticklabels = [t.get_text() if hasattr(t, 'get_text') else t | ||
| 1736 | + for t in ticklabels] | ||
| 1733 | 1737 | if minor: | |
| 1734 | 1738 | self.set_minor_formatter(mticker.FixedFormatter(ticklabels)) | |
| 1735 | 1739 | ticks = self.get_minor_ticks() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -345,6 +345,11 @@ def __call__(self, x, pos=None): | |||
| 345 | 345 | class FixedFormatter(Formatter): | |
| 346 | 346 | """ | |
| 347 | 347 | Return fixed strings for tick labels based only on position, not value. | |
| 348 | + | ||
| 349 | + .. note:: | ||
| 350 | + `.FixedFormatter` should only be used together with `.FixedLocator`. | ||
| 351 | + Otherwise, the labels may end up in unexpected positions. | ||
| 352 | + | ||
| 348 | 353 | """ | |
| 349 | 354 | def __init__(self, seq): | |
| 350 | 355 | """ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments