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

Skip to content

Commit 635684f

Browse files
committed
some unit cleanup; warn on zero value dates in the formatter
svn path=/trunk/matplotlib/; revision=7802
1 parent 8c3218b commit 635684f

5 files changed

Lines changed: 32 additions & 15 deletions

File tree

lib/matplotlib/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ def test(verbosity=0):
900900
backend = rcParams['backend']
901901
original_params = rcParams.copy()
902902

903-
use('Agg') # use Agg backend for these tests
903+
use('Agg', warn=False) # use Agg backend for these tests
904904

905905
# These settings *must* be hardcoded for running the comparison
906906
# tests and are not necessarily the default values as specified in
@@ -922,7 +922,7 @@ def test(verbosity=0):
922922
)
923923

924924
# restore the old backend and rcParams
925-
use(backend)
925+
use(backend, warn=False)
926926
rcParams.update(original_params)
927927
return success
928928

lib/matplotlib/axes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,16 +2306,14 @@ def xaxis_date(self, tz=None):
23062306
"""
23072307
# should be enough to inform the unit conversion interface
23082308
# dates are comng in
2309-
self.xaxis.update_units(datetime.date(2009,1,1))
2309+
self.xaxis.axis_date()
23102310

23112311
def yaxis_date(self, tz=None):
23122312
"""Sets up y-axis ticks and labels that treat the y data as dates.
23132313
23142314
*tz* is the time zone to use in labeling dates. Defaults to rc value.
23152315
"""
2316-
# should be enough to inform the unit conversion interface
2317-
# dates are comng in
2318-
self.yaxis.update_units(datetime.date(2009,1,1))
2316+
self.yaxis.axis_date()
23192317

23202318
def format_xdata(self, x):
23212319
"""

lib/matplotlib/axis.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -987,12 +987,17 @@ def update_units(self, data):
987987

988988
converter = munits.registry.get_converter(data)
989989
if converter is None: return False
990+
991+
neednew = self.converter!=converter
990992
self.converter = converter
991993
default = self.converter.default_units(data, self)
992994
#print 'update units: default="%s", units=%s"'%(default, self.units)
993995
if default is not None and self.units is None:
994996
self.set_units(default)
995-
self._update_axisinfo()
997+
998+
999+
if neednew:
1000+
self._update_axisinfo()
9961001
return True
9971002

9981003
def _update_axisinfo(self):
@@ -1196,6 +1201,17 @@ def zoom(self, direction):
11961201
"Zoom in/out on axis; if *direction* is >0 zoom in, else zoom out"
11971202
self.major.locator.zoom(direction)
11981203

1204+
1205+
def axis_date(self):
1206+
"""
1207+
Sets up x-axis ticks and labels that treat the x data as dates.
1208+
"""
1209+
import datetime
1210+
# should be enough to inform the unit conversion interface
1211+
# dates are comng in
1212+
self.update_units(datetime.date(2009,1,1))
1213+
1214+
11991215
class XAxis(Axis):
12001216
__name__ = 'xaxis'
12011217
axis_name = 'x'
@@ -1442,7 +1458,7 @@ def set_default_intervals(self):
14421458
if not dataMutated or not viewMutated:
14431459
if self.converter is not None:
14441460
info = self.converter.axisinfo(self.units, self)
1445-
if info.default_limits is not None:
1461+
if info.default_limits is not None:
14461462
valmin, valmax = info.default_limits
14471463
xmin = self.converter.convert(valmin, self.units, self)
14481464
xmax = self.converter.convert(valmax, self.units, self)
@@ -1451,7 +1467,7 @@ def set_default_intervals(self):
14511467
if not viewMutated:
14521468
self.axes.viewLim.intervalx = xmin, xmax
14531469

1454-
1470+
14551471

14561472
class YAxis(Axis):
14571473
__name__ = 'yaxis'
@@ -1707,7 +1723,7 @@ def set_default_intervals(self):
17071723
if not dataMutated or not viewMutated:
17081724
if self.converter is not None:
17091725
info = self.converter.axisinfo(self.units, self)
1710-
if info.default_limits is not None:
1726+
if info.default_limits is not None:
17111727
valmin, valmax = info.default_limits
17121728
ymin = self.converter.convert(valmin, self.units, self)
17131729
ymax = self.converter.convert(valmax, self.units, self)
@@ -1716,4 +1732,4 @@ def set_default_intervals(self):
17161732
if not viewMutated:
17171733
self.axes.viewLim.intervaly = ymin, ymax
17181734

1719-
1735+

lib/matplotlib/cbook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def is_scalar(obj):
317317
def is_numlike(obj):
318318
'return true if *obj* looks like a number'
319319
try: obj+1
320-
except TypeError: return False
320+
except: return False
321321
else: return True
322322

323323
def to_filehandle(fname, flag='rU', return_opened=False):

lib/matplotlib/dates.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ def __init__(self, fmt, tz=None):
290290
self.tz = tz
291291

292292
def __call__(self, x, pos=0):
293+
if x==0:
294+
raise ValueError('DateFormatter found a value of x=0, which is an illegal date. This usually occurs because you have not informed the axis that it is plotting dates, eg with ax.xaxis_date()')
293295
dt = num2date(x, self.tz)
294296
return self.strftime(dt, self.fmt)
295297

@@ -430,6 +432,7 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
430432
}
431433

432434
def __call__(self, x, pos=0):
435+
433436
scale = float( self._locator._get_unit() )
434437

435438
fmt = self.defaultfmt
@@ -1065,10 +1068,10 @@ def axisinfo(unit, axis):
10651068

10661069
majloc = AutoDateLocator(tz=unit)
10671070
majfmt = AutoDateFormatter(majloc, tz=unit)
1068-
datemin = datetime.date(2000, 1, 1)
1069-
datemax = datetime.date(2010, 1, 1)
1071+
datemin = datetime.date(2000, 1, 1)
1072+
datemax = datetime.date(2010, 1, 1)
10701073

1071-
return units.AxisInfo( majloc=majloc, majfmt=majfmt, label='',
1074+
return units.AxisInfo( majloc=majloc, majfmt=majfmt, label='',
10721075
default_limits=(datemin, datemax))
10731076

10741077
@staticmethod

0 commit comments

Comments
 (0)