@@ -1529,37 +1529,93 @@ def set_major_formatter(self, formatter):
15291529 """
15301530 Set the formatter of the major ticker.
15311531
1532+ In addition to a `~matplotlib.ticker.Formatter` instance,
1533+ this also accepts a `str` or function.
1534+
1535+ For a `str` a `~matplotlib.ticker.StrMethodFormatter` is used.
1536+ The field used for the value must be labeled *x* and the field used
1537+ for the position must be labeled *pos*.
1538+
1539+ For a function a `~matplotlib.ticker.FuncFormatter` is used.
1540+ The function should take in two inputs (a tick value ``x`` and a
1541+ position ``pos``), and return a string containing the corresponding
1542+ tick label.
1543+
15321544 Parameters
15331545 ----------
1534- formatter : `~matplotlib.ticker.Formatter`
1546+ formatter : `~matplotlib.ticker.Formatter`, str, or function
15351547 """
1536- cbook ._check_isinstance (mticker .Formatter , formatter = formatter )
1537- if (isinstance (formatter , mticker .FixedFormatter )
1538- and len (formatter .seq ) > 0
1539- and not isinstance (self .major .locator , mticker .FixedLocator )):
1540- cbook ._warn_external ('FixedFormatter should only be used together '
1541- 'with FixedLocator' )
1542- self .isDefault_majfmt = False
1543- self .major .formatter = formatter
1544- formatter .set_axis (self )
1545- self .stale = True
1548+ self ._set_formatter (formatter , self .major )
15461549
15471550 def set_minor_formatter (self , formatter ):
15481551 """
15491552 Set the formatter of the minor ticker.
15501553
1554+ In addition to a `~matplotlib.ticker.Formatter` instance,
1555+ this also accepts a `str` or function.
1556+
1557+ For a `str` a `~matplotlib.ticker.StrMethodFormatter` is used.
1558+ The field used for the tick value must be labeled *x* and the field
1559+ used for the tick position must be labeled *pos*.
1560+ See the `~matplotlib.ticker.StrMethodFormatter` documentation for
1561+ more information.
1562+
1563+ For a function a `~matplotlib.ticker.FuncFormatter` is used.
1564+ The function should take in two inputs (a tick value ``x`` and a
1565+ position ``pos``), and return a string containing the corresponding
1566+ tick label.
1567+ See the `~matplotlib.ticker.FuncFormatter` documentation for
1568+ more information.
1569+
15511570 Parameters
15521571 ----------
1553- formatter : `~matplotlib.ticker.Formatter`
1572+ formatter : `~matplotlib.ticker.Formatter`, str, or function
15541573 """
1555- cbook ._check_isinstance (mticker .Formatter , formatter = formatter )
1574+ self ._set_formatter (formatter , self .minor )
1575+
1576+ def _set_formatter (self , formatter , level ):
1577+ """
1578+ Set the formatter of the major or minor ticker.
1579+
1580+ In addition to a `~matplotlib.ticker.Formatter` instance,
1581+ this also accepts a `str` or function.
1582+
1583+ For a `str` a `~matplotlib.ticker.StrMethodFormatter` is used.
1584+ The field used for the value must be labeled *x* and the field used
1585+ for the position must be labeled *pos*.
1586+
1587+ For a function a `~matplotlib.ticker.FuncFormatter` is used.
1588+ The function should take in two inputs (a tick value ``x`` and a
1589+ position ``pos``), and return a string containing the corresponding
1590+ tick label.
1591+
1592+ Parameters
1593+ ----------
1594+ formatter : `~matplotlib.ticker.Formatter`, str, or function
1595+ level : `self.major` or `self.minor`
1596+ """
1597+ if isinstance (formatter , str ):
1598+ formatter = mticker .StrMethodFormatter (formatter )
1599+ # Don't allow any other TickHelper to avoid easy-to-make errors,
1600+ # like using a Locator instead of a Formatter.
1601+ elif (callable (formatter ) and
1602+ not isinstance (formatter , mticker .TickHelper )):
1603+ formatter = mticker .FuncFormatter (formatter )
1604+ else :
1605+ cbook ._check_isinstance (mticker .Formatter , formatter = formatter )
1606+
15561607 if (isinstance (formatter , mticker .FixedFormatter )
15571608 and len (formatter .seq ) > 0
1558- and not isinstance (self . minor .locator , mticker .FixedLocator )):
1609+ and not isinstance (level .locator , mticker .FixedLocator )):
15591610 cbook ._warn_external ('FixedFormatter should only be used together '
15601611 'with FixedLocator' )
1561- self .isDefault_minfmt = False
1562- self .minor .formatter = formatter
1612+
1613+ if level == self .major :
1614+ self .isDefault_majfmt = False
1615+ else :
1616+ self .isDefault_minfmt = False
1617+
1618+ level .formatter = formatter
15631619 formatter .set_axis (self )
15641620 self .stale = True
15651621
0 commit comments