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

Method format_ticks

lib/matplotlib/dates.py:733–815  ·  view source on GitHub ↗
(self, values)

Source from the content-addressed store, hash-verified

731 return formatter(x, pos=pos)
732
733 def format_ticks(self, values):
734 tickdatetime = [num2date(value, tz=self._tz) for value in values]
735 tickdate = np.array([tdt.timetuple()[:6] for tdt in tickdatetime])
736
737 # basic algorithm:
738 # 1) only display a part of the date if it changes over the ticks.
739 # 2) don't display the smaller part of the date if:
740 # it is always the same or if it is the start of the
741 # year, month, day etc.
742 # fmt for most ticks at this level
743 fmts = self.formats
744 # format beginnings of days, months, years, etc.
745 zerofmts = self.zero_formats
746 # offset fmt are for the offset in the upper left of the
747 # or lower right of the axis.
748 offsetfmts = self.offset_formats
749 show_offset = self.show_offset
750
751 # determine the level we will label at:
752 # mostly 0: years, 1: months, 2: days,
753 # 3: hours, 4: minutes, 5: seconds, 6: microseconds
754 for level in range(5, -1, -1):
755 unique = np.unique(tickdate[:, level])
756 if len(unique) > 1:
757 # if 1 is included in unique, the year is shown in ticks
758 if level < 2 and np.any(unique == 1):
759 show_offset = False
760 break
761 elif level == 0:
762 # all tickdate are the same, so only micros might be different
763 # set to the most precise (6: microseconds doesn't exist...)
764 level = 5
765
766 # level is the basic level we will label at.
767 # now loop through and decide the actual ticklabels
768 zerovals = [0, 1, 1, 0, 0, 0, 0]
769 labels = [''] * len(tickdate)
770 for nn in range(len(tickdate)):
771 if level < 5:
772 if tickdate[nn][level] == zerovals[level]:
773 fmt = zerofmts[level]
774 else:
775 fmt = fmts[level]
776 else:
777 # special handling for seconds + microseconds
778 if (tickdatetime[nn].second == tickdatetime[nn].microsecond
779 == 0):
780 fmt = zerofmts[level]
781 else:
782 fmt = fmts[level]
783 labels[nn] = tickdatetime[nn].strftime(fmt)
784
785 # special handling of seconds and microseconds:
786 # strip extra zeros and decimal if possible.
787 # this is complicated by two factors. 1) we have some level-4 strings
788 # here (i.e. 03:00, '0.50000', '1.000') 2) we would like to have the
789 # same number of decimals for each string (i.e. 0.5 and 1.0).
790 if level >= 5:

Callers 2

Calls 3

num2dateFunction · 0.85
_wrap_in_texFunction · 0.85
get_invertedMethod · 0.80

Tested by 2