33"""
44
55import datetime
6+ import functools
67import logging
78
89import numpy as np
@@ -1646,6 +1647,11 @@ def set_pickradius(self, pickradius):
16461647 """
16471648 self .pickradius = pickradius
16481649
1650+ # Helper for set_ticklabels. Defining it here makes it pickleable.
1651+ @staticmethod
1652+ def _format_with_dict (tickd , x , pos ):
1653+ return tickd .get (x , "" )
1654+
16491655 def set_ticklabels (self , ticklabels , * , minor = False , ** kwargs ):
16501656 r"""
16511657 Set the text values of the tick labels.
@@ -1658,8 +1664,9 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
16581664 Parameters
16591665 ----------
16601666 ticklabels : sequence of str or of `.Text`\s
1661- List of texts for tick labels; must include values for non-visible
1662- labels.
1667+ Texts for labeling each tick location in the sequence set by
1668+ `.Axis.set_ticks`; the number of labels must match the number of
1669+ locations.
16631670 minor : bool
16641671 If True, set minor ticks instead of major ticks.
16651672 **kwargs
@@ -1673,14 +1680,34 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
16731680 """
16741681 ticklabels = [t .get_text () if hasattr (t , 'get_text' ) else t
16751682 for t in ticklabels ]
1683+ locator = (self .get_minor_locator () if minor
1684+ else self .get_major_locator ())
1685+ if isinstance (locator , mticker .FixedLocator ):
1686+ if len (locator .locs ) != len (ticklabels ):
1687+ raise ValueError (
1688+ "The number of FixedLocator locations"
1689+ f" ({ len (locator .locs )} ), usually from a call to"
1690+ " set_ticks, does not match"
1691+ f" the number of ticklabels ({ len (ticklabels )} )." )
1692+ tickd = {loc : lab for loc , lab in zip (locator .locs , ticklabels )}
1693+ func = functools .partial (self ._format_with_dict , tickd )
1694+ formatter = mticker .FuncFormatter (func )
1695+ else :
1696+ formatter = mticker .FixedFormatter (ticklabels )
1697+
16761698 if minor :
1677- self .set_minor_formatter (mticker .FixedFormatter (ticklabels ))
1678- ticks = self .get_minor_ticks ()
1699+ self .set_minor_formatter (formatter )
1700+ locs = self .get_minorticklocs ()
1701+ ticks = self .get_minor_ticks (len (locs ))
16791702 else :
1680- self .set_major_formatter (mticker .FixedFormatter (ticklabels ))
1681- ticks = self .get_major_ticks ()
1703+ self .set_major_formatter (formatter )
1704+ locs = self .get_majorticklocs ()
1705+ ticks = self .get_major_ticks (len (locs ))
1706+
16821707 ret = []
1683- for tick_label , tick in zip (ticklabels , ticks ):
1708+ for pos , (loc , tick ) in enumerate (zip (locs , ticks )):
1709+ tick .update_position (loc )
1710+ tick_label = formatter (loc , pos )
16841711 # deal with label1
16851712 tick .label1 .set_text (tick_label )
16861713 tick .label1 .update (kwargs )
0 commit comments