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

Skip to content

Commit 2d045e8

Browse files
author
James Evans
committed
User specified tickers and labels for any given axis will now take precedence over "default" values.
svn path=/trunk/matplotlib/; revision=6870
1 parent 5a518d6 commit 2d045e8

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

lib/matplotlib/axes.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,12 +2672,7 @@ def set_xlabel(self, xlabel, fontdict=None, **kwargs):
26722672
:meth:`text`
26732673
for information on how override and the optional args work
26742674
"""
2675-
2676-
label = self.xaxis.get_label()
2677-
label.set_text(xlabel)
2678-
if fontdict is not None: label.update(fontdict)
2679-
label.update(kwargs)
2680-
return label
2675+
return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
26812676
set_xlabel.__doc__ = cbook.dedent(set_xlabel.__doc__) % martist.kwdocd
26822677

26832678
def get_ylabel(self):
@@ -2704,11 +2699,7 @@ def set_ylabel(self, ylabel, fontdict=None, **kwargs):
27042699
:meth:`text`
27052700
for information on how override and the optional args work
27062701
"""
2707-
label = self.yaxis.get_label()
2708-
label.set_text(ylabel)
2709-
if fontdict is not None: label.update(fontdict)
2710-
label.update(kwargs)
2711-
return label
2702+
return self.yaxis.set_label_text(ylabel, fontdict, **kwargs)
27122703
set_ylabel.__doc__ = cbook.dedent(set_ylabel.__doc__) % martist.kwdocd
27132704

27142705
def text(self, x, y, s, fontdict=None,

lib/matplotlib/axis.py

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,15 @@ def __init__(self, axes, pickradius=15):
514514
artist.Artist.__init__(self)
515515
self.set_figure(axes.figure)
516516

517+
# Keep track of setting to the default value, this allows use to know
518+
# if any of the following values is explicitly set by the user, so as
519+
# to not overwrite their settings with any of our 'auto' settings.
520+
self.isDefault_majloc = True
521+
self.isDefault_minloc = True
522+
self.isDefault_majfmt = True
523+
self.isDefault_minfmt = True
524+
self.isDefault_label = True
525+
517526
self.axes = axes
518527
self.major = Ticker()
519528
self.minor = Ticker()
@@ -568,6 +577,11 @@ def set_scale(self, value, **kwargs):
568577
self._scale = mscale.scale_factory(value, self, **kwargs)
569578
self._scale.set_default_locators_and_formatters(self)
570579

580+
self.isDefault_majloc = True
581+
self.isDefault_minloc = True
582+
self.isDefault_majfmt = True
583+
self.isDefault_minfmt = True
584+
571585
def limit_range_for_scale(self, vmin, vmax):
572586
return self._scale.limit_range_for_scale(vmin, vmax, self.get_minpos())
573587

@@ -587,6 +601,18 @@ def cla(self):
587601
self.set_minor_locator(mticker.NullLocator())
588602
self.set_minor_formatter(mticker.NullFormatter())
589603

604+
self.set_label_text('')
605+
self._set_artist_props(self.label)
606+
607+
# Keep track of setting to the default value, this allows use to know
608+
# if any of the following values is explicitly set by the user, so as
609+
# to not overwrite their settings with any of our 'auto' settings.
610+
self.isDefault_majloc = True
611+
self.isDefault_minloc = True
612+
self.isDefault_majfmt = True
613+
self.isDefault_minfmt = True
614+
self.isDefault_label = True
615+
590616
# Clear the callback registry for this axis, or it may "leak"
591617
self.callbacks = cbook.CallbackRegistry(('units', 'units finalize'))
592618

@@ -836,6 +862,10 @@ def _copy_tick_props(self, src, dest):
836862
dest.label1On = src.label1On
837863
dest.label2On = src.label2On
838864

865+
def get_label_text(self):
866+
'Get the text of the label'
867+
return self.label.get_text()
868+
839869
def get_major_locator(self):
840870
'Get the locator of the major ticker'
841871
return self.major.locator
@@ -958,17 +988,21 @@ def _update_axisinfo(self):
958988
info = self.converter.axisinfo(self.units, self)
959989
if info is None:
960990
return
961-
if info.majloc is not None and self.major.locator!=info.majloc:
991+
if info.majloc is not None and self.major.locator!=info.majloc and self.isDefault_majloc:
962992
self.set_major_locator(info.majloc)
963-
if info.minloc is not None and self.minor.locator!=info.minloc:
993+
self.isDefault_majloc = True
994+
if info.minloc is not None and self.minor.locator!=info.minloc and self.isDefault_minloc:
964995
self.set_minor_locator(info.minloc)
965-
if info.majfmt is not None and self.major.formatter!=info.majfmt:
996+
self.isDefault_minloc = True
997+
if info.majfmt is not None and self.major.formatter!=info.majfmt and self.isDefault_majfmt:
966998
self.set_major_formatter(info.majfmt)
967-
if info.minfmt is not None and self.minor.formatter!=info.minfmt:
999+
self.isDefault_majfmt = True
1000+
if info.minfmt is not None and self.minor.formatter!=info.minfmt and self.isDefault_minfmt:
9681001
self.set_minor_formatter(info.minfmt)
969-
if info.label is not None:
970-
label = self.get_label()
971-
label.set_text(info.label)
1002+
self.isDefault_minfmt = True
1003+
if info.label is not None and self.isDefault_label:
1004+
self.set_label_text(info.label)
1005+
self.isDefault_label = True
9721006

9731007

9741008
def have_units(self):
@@ -1010,12 +1044,24 @@ def get_units(self):
10101044
'return the units for axis'
10111045
return self.units
10121046

1047+
def set_label_text(self, label, fontdict = None, **kwargs):
1048+
""" Sets the text value of the axis label
1049+
1050+
ACCEPTS: A string value for the label
1051+
"""
1052+
self.isDefault_label = False
1053+
self.label.set_text(label)
1054+
if fontdict is not None: self.label.update(fontdict)
1055+
self.label.update(kwargs)
1056+
return self.label
1057+
10131058
def set_major_formatter(self, formatter):
10141059
"""
10151060
Set the formatter of the major ticker
10161061
10171062
ACCEPTS: A :class:`~matplotlib.ticker.Formatter` instance
10181063
"""
1064+
self.isDefault_majfmt = False
10191065
self.major.formatter = formatter
10201066
formatter.set_axis(self)
10211067

@@ -1026,6 +1072,7 @@ def set_minor_formatter(self, formatter):
10261072
10271073
ACCEPTS: A :class:`~matplotlib.ticker.Formatter` instance
10281074
"""
1075+
self.isDefault_minfmt = False
10291076
self.minor.formatter = formatter
10301077
formatter.set_axis(self)
10311078

@@ -1036,6 +1083,7 @@ def set_major_locator(self, locator):
10361083
10371084
ACCEPTS: a :class:`~matplotlib.ticker.Locator` instance
10381085
"""
1086+
self.isDefault_majloc = False
10391087
self.major.locator = locator
10401088
locator.set_axis(self)
10411089

@@ -1046,6 +1094,7 @@ def set_minor_locator(self, locator):
10461094
10471095
ACCEPTS: a :class:`~matplotlib.ticker.Locator` instance
10481096
"""
1097+
self.isDefault_minloc = False
10491098
self.minor.locator = locator
10501099
locator.set_axis(self)
10511100

0 commit comments

Comments
 (0)