Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit feaf6d6

Browse files
committed
Added easy access to minor tick properties (Pierre G-M)
svn path=/trunk/matplotlib/; revision=4106
1 parent 6651210 commit feaf6d6

File tree

2 files changed

+100
-41
lines changed

2 files changed

+100
-41
lines changed

lib/matplotlib/axes.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ def update_datalim(self, xys):
11731173
# Otherwise, it will compute the bounds of it's current data
11741174
# and the data in xydata
11751175
xys = npy.asarray(xys)
1176-
1176+
11771177

11781178
self.dataLim.update_numerix_xy(xys, -1)
11791179

@@ -1631,25 +1631,33 @@ def set_xscale(self, value, basex = 10, subsx=None):
16311631
self.xaxis.set_minor_formatter(mticker.NullFormatter())
16321632
self.transData.get_funcx().set_type( mtrans.IDENTITY )
16331633

1634-
def get_xticks(self):
1634+
def get_xticks(self, minor=False):
16351635
'Return the x ticks as a list of locations'
1636-
return self.xaxis.get_ticklocs()
1636+
return self.xaxis.get_ticklocs(minor=minor)
16371637

1638-
def set_xticks(self, ticks):
1638+
def set_xticks(self, ticks, minor=False):
16391639
"""
16401640
Set the x ticks with list of ticks
16411641
16421642
ACCEPTS: sequence of floats
16431643
"""
1644-
return self.xaxis.set_ticks(ticks)
1644+
return self.xaxis.set_ticks(ticks, minor=minor)
1645+
1646+
def get_xmajorticklabels(self):
1647+
'Get the xtick labels as a list of Text instances'
1648+
return cbook.silent_list('Text xticklabel', self.xaxis.get_majorticklabels())
16451649

1646-
def get_xticklabels(self):
1650+
def get_xminorticklabels(self):
16471651
'Get the xtick labels as a list of Text instances'
1648-
return cbook.silent_list('Text xticklabel', self.xaxis.get_ticklabels())
1652+
return cbook.silent_list('Text xticklabel', self.xaxis.get_minorticklabels())
16491653

1650-
def set_xticklabels(self, labels, fontdict=None, **kwargs):
1654+
def get_xticklabels(self, minor=False):
1655+
'Get the xtick labels as a list of Text instances'
1656+
return cbook.silent_list('Text xticklabel', self.xaxis.get_ticklabels(minor=minor))
1657+
1658+
def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs):
16511659
"""
1652-
SET_XTICKLABELS(labels, fontdict=None, **kwargs)
1660+
set_xticklabels(labels, fontdict=None, minor=False, **kwargs)
16531661
16541662
Set the xtick labels with list of strings labels Return a list of axis
16551663
text instances.
@@ -1659,7 +1667,7 @@ def set_xticklabels(self, labels, fontdict=None, **kwargs):
16591667
16601668
ACCEPTS: sequence of strings
16611669
"""
1662-
return self.xaxis.set_ticklabels(labels, fontdict, **kwargs)
1670+
return self.xaxis.set_ticklabels(labels, fontdict, minor=minor, **kwargs)
16631671
set_xticklabels.__doc__ = cbook.dedent(set_xticklabels.__doc__) % martist.kwdocd
16641672

16651673
def invert_yaxis(self):
@@ -1795,25 +1803,33 @@ def set_yscale(self, value, basey=10, subsy=None):
17951803
self.yaxis.set_minor_formatter(mticker.NullFormatter())
17961804
self.transData.get_funcy().set_type( mtrans.IDENTITY )
17971805

1798-
def get_yticks(self):
1806+
def get_yticks(self, minor=False):
17991807
'Return the y ticks as a list of locations'
1800-
return self.yaxis.get_ticklocs()
1808+
return self.yaxis.get_ticklocs(minor=minor)
18011809

1802-
def set_yticks(self, ticks):
1810+
def set_yticks(self, ticks, minor=False):
18031811
"""
18041812
Set the y ticks with list of ticks
18051813
18061814
ACCEPTS: sequence of floats
18071815
"""
1808-
return self.yaxis.set_ticks(ticks)
1816+
return self.yaxis.set_ticks(ticks, minor=minor)
18091817

1810-
def get_yticklabels(self):
1811-
'Get the ytick labels as a list of Text instances'
1812-
return cbook.silent_list('Text yticklabel', self.yaxis.get_ticklabels())
1818+
def get_ymajorticklabels(self):
1819+
'Get the xtick labels as a list of Text instances'
1820+
return cbook.silent_list('Text yticklabel', self.yaxis.get_majorticklabels())
18131821

1814-
def set_yticklabels(self, labels, fontdict=None, **kwargs):
1822+
def get_yminorticklabels(self):
1823+
'Get the xtick labels as a list of Text instances'
1824+
return cbook.silent_list('Text yticklabel', self.yaxis.get_minorticklabels())
1825+
1826+
def get_yticklabels(self, minor=False):
1827+
'Get the xtick labels as a list of Text instances'
1828+
return cbook.silent_list('Text yticklabel', self.yaxis.get_ticklabels(minor=minor))
1829+
1830+
def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):
18151831
"""
1816-
SET_YTICKLABELS(labels, fontdict=None, **kwargs)
1832+
set_yticklabels(labels, fontdict=None, minor=False, **kwargs)
18171833
18181834
Set the ytick labels with list of strings labels. Return a list of
18191835
Text instances.
@@ -1823,7 +1839,7 @@ def set_yticklabels(self, labels, fontdict=None, **kwargs):
18231839
18241840
ACCEPTS: sequence of strings
18251841
"""
1826-
return self.yaxis.set_ticklabels(labels, fontdict, **kwargs)
1842+
return self.yaxis.set_ticklabels(labels, fontdict, minor=minor, **kwargs)
18271843
set_yticklabels.__doc__ = cbook.dedent(set_yticklabels.__doc__) % martist.kwdocd
18281844

18291845
def toggle_log_lineary(self):
@@ -3702,7 +3718,7 @@ def xywhere(xs, ys, mask):
37023718

37033719
if xerr is not None:
37043720
if iterable(xerr) and len(xerr)==2:
3705-
# using list comps rather than arrays to preserve units
3721+
# using list comps rather than arrays to preserve units
37063722
left = [thisx-thiserr for (thisx, thiserr) in cbook.safezip(x,xerr[0])]
37073723
right = [thisx+thiserr for (thisx, thiserr) in cbook.safezip(x,xerr[1])]
37083724
else:

lib/matplotlib/axis.py

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ def __init__(self, axes, pickradius=15):
520520
def get_children(self):
521521
children = [self.label]
522522
majorticks = self.get_major_ticks()
523-
minorticks = self.get_minor_ticks()
524-
523+
minorticks = self.get_minor_ticks()
524+
525525
children.extend(majorticks)
526526
children.extend(minorticks)
527527
return children
@@ -661,24 +661,62 @@ def get_pickradius(self):
661661
'Return the depth of the axis used by the picker'
662662
return self.pickradius
663663

664-
def get_ticklabels(self):
665-
'Return a list of Text instances for ticklabels'
664+
def get_majorticklabels(self):
665+
'Return a list of Text instances for the major ticklabels'
666666
ticks = self.get_major_ticks()
667667
labels1 = [tick.label1 for tick in ticks if tick.label1On]
668668
labels2 = [tick.label2 for tick in ticks if tick.label2On]
669-
return silent_list('Text ticklabel', labels1+labels2)
669+
return silent_list('Text major ticklabel', labels1+labels2)
670+
671+
def get_minorticklabels(self):
672+
'Return a list of Text instances for the minor ticklabels'
673+
ticks = self.get_minor_ticks()
674+
labels1 = [tick.label1 for tick in ticks if tick.label1On]
675+
labels2 = [tick.label2 for tick in ticks if tick.label2On]
676+
return silent_list('Text minor ticklabel', labels1+labels2)
677+
678+
def get_ticklabels(self, minor=False):
679+
'Return a list of Text instances for ticklabels'
680+
if minor:
681+
return self.get_minorticklabels()
682+
return self.get_majorticklabels()
683+
684+
def get_majorticklines(self):
685+
'Return the major tick lines as a list of Line2D instances'
686+
lines = []
687+
ticks = self.get_major_ticks()
688+
for tick in ticks:
689+
lines.append(tick.tick1line)
690+
lines.append(tick.tick2line)
691+
return silent_list('Line2D ticklines', lines)
670692

671-
def get_ticklines(self):
672-
'Return the ticklines lines as a list of Line2D instance'
693+
def get_minorticklines(self):
694+
'Return the minor tick lines as a list of Line2D instances'
673695
lines = []
674-
ticks = self.get_major_ticks()
696+
ticks = self.get_minor_ticks()
675697
for tick in ticks:
676698
lines.append(tick.tick1line)
677699
lines.append(tick.tick2line)
678700
return silent_list('Line2D ticklines', lines)
679701

680-
def get_ticklocs(self):
702+
def get_ticklines(self, minor=False):
703+
'Return the tick lines as a list of Line2D instances'
704+
if minor:
705+
return self.get_minorticklines()
706+
return self.get_majorticklines()
707+
708+
def get_majorticklocs(self):
709+
"Get the major tick locations in data coordinates as a numpy array"
710+
return self.major.locator()
711+
712+
def get_minorticklocs(self):
713+
"Get the minor tick locations in data coordinates as a numpy array"
714+
return self.minor.locator()
715+
716+
def get_ticklocs(self, minor=False):
681717
"Get the tick locations in data coordinates as a numpy array"
718+
if minor:
719+
return self.minor.locator()
682720
return self.major.locator()
683721

684722
def _get_tick(self, major):
@@ -718,15 +756,12 @@ def get_minor_formatter(self):
718756

719757
def get_major_ticks(self):
720758
'get the tick instances; grow as necessary'
721-
722759
numticks = len(self.major.locator())
723-
724760
if len(self.majorTicks)<numticks:
725761
# update the new tick label properties from the old
726762
protoTick = self.majorTicks[0]
727763
for i in range(numticks-len(self.majorTicks)):
728764
tick = self._get_tick(major=True)
729-
#tick = protoTick
730765
if self._gridOnMajor: tick.gridOn = True
731766
self._copy_tick_props(protoTick, tick)
732767
self.majorTicks.append(tick)
@@ -915,34 +950,42 @@ def set_pickradius(self, pickradius):
915950
def set_ticklabels(self, ticklabels, *args, **kwargs):
916951
"""
917952
Set the text values of the tick labels. Return a list of Text
918-
instances.
953+
instances. Use kwarg minor=True to select minor ticks.
919954
920955
ACCEPTS: sequence of strings
921956
"""
922957
#ticklabels = [str(l) for l in ticklabels]
923-
924-
self.set_major_formatter( FixedFormatter(ticklabels) )
925-
958+
minor = kwargs.pop('minor', False)
959+
if minor:
960+
self.set_minor_formatter(FixedFormatter(ticklabels))
961+
ticks = self.get_minor_ticks()
962+
else:
963+
self.set_major_formatter( FixedFormatter(ticklabels) )
964+
ticks = self.get_major_ticks()
926965

927966
ret = []
928-
for i, tick in enumerate(self.get_major_ticks()):
967+
for i, tick in enumerate(ticks):
929968
if i<len(ticklabels):
930969
tick.label1.set_text(ticklabels[i])
931970
ret.append(tick.label1)
932971
tick.label1.update(kwargs)
933972
return ret
934973

935-
def set_ticks(self, ticks):
974+
def set_ticks(self, ticks, minor=False):
936975
"""
937976
Set the locations of the tick marks from sequence ticks
938977
939978
ACCEPTS: sequence of floats
940979
"""
941980
### XXX if the user changes units, the information will be lost here
942981
ticks = self.convert_units(ticks)
943-
self.set_major_locator( FixedLocator(ticks) )
944982
self.get_view_interval().update(ticks,0)
945-
return self.get_major_ticks()
983+
if minor:
984+
self.set_minor_locator(FixedLocator(ticks))
985+
return self.get_minor_ticks()
986+
else:
987+
self.set_major_locator( FixedLocator(ticks) )
988+
return self.get_major_ticks()
946989

947990
def _update_label_position(self, bboxes, bboxes2):
948991
"""

0 commit comments

Comments
 (0)