我之前写过的一个python脚本突然停止工作,我将matplotlib库从版本升级到0.99.1.1'版本' 1.2.x'。
我正在使用python v 2.6.5
经过几个小时的调试,我终于找到了导致问题的线路。它们分为以下两行:
# Note: ax is an Axis type, phist is an iterable type
def function_no_longer_works(phist):
# some code before ...
ax.xaxis.set_major_locator(_MajorLocator(phist))
ax.xaxis.set_major_formatter( FuncFormatter(_MajorFormatter(phist).format)
# some code after ...
class _CanFit(object):
def __init__(self, phist):
self.phist = phist
def weeks(self):
if (self.phist[-1].date - self.phist[0].date).days > 5*30:
# 5 months at most
return False
return True
def months(self):
if (self.phist[-1].date - self.phist[0].date).days > 3*365:
# 3 years at most
return False
return True
class _MajorLocator(object):
"""calculates the positions of months or years on the y-axis
These positions will be where the major ticks and labels for them are.
If months can't fit, there are no major ticks, only minor ticks.
"""
def __init__(self, phist):
self.phist = phist
def set_axis(self, axis): pass
def view_limits(self, start, stop): pass
def __call__(self):
"""returns an iterable of all the months or years on the y-axis"""
can_fit = _CanFit(self.phist)
major_ticks = []
if can_fit.weeks():
month = None
# step through the price history list and find the index of every
# point where the previous point has a different month
for (i, p) in enumerate(self.phist):
if month != p.date.month:
if month is not None:
major_ticks.append(i)
month = p.date.month
elif can_fit.months():
year = None
# same as above, but for years.
for (i, p) in enumerate(self.phist):
if year != p.date.year:
if year is not None:
major_ticks.append(i)
year = p.date.year
return major_ticks
class _MajorFormatter(object):
"""Formats the major ticks as years or months"""
def __init__(self, phist):
self.phist = phist
def format(self, x, pos=None):
can_fit = _CanFit(self.phist)
if can_fit.weeks():
# Jan Feb Mar etc
return self.phist[x].date.strftime("%b")
if can_fit.months():
# 90, 91, etc
return self.phist[x].date.strftime("%y")
当我使用matplot lib v 1.2.x运行脚本时,这是堆栈跟踪:
/usr/local/lib/python2.6/dist-packages/matplotlib/axes.pyc in plot(self, *args, **kwargs)
3851
3852
-> 3853 self.autoscale_view(scalex=scalex, scaley=scaley)
3854 return lines
3855
/usr/local/lib/python2.6/dist-packages/matplotlib/axes.pyc in autoscale_view(self, tight, scalex, scaley)
1840 x1 += delta
1841 if not _tight:
-> 1842 x0, x1 = xlocator.view_limits(x0, x1)
1843 self.set_xbound(x0, x1)
1844
TypeError: 'NoneType' object is not iterable
过去几天我绞尽脑汁后,我的想法已经用完了。我们非常感谢您解决此问题的任何帮助。
答案 0 :(得分:0)
您的问题是您没有在_MajorLocator.view_limits
中返回任何内容。
预计会使用view_limits
实现某种自动缩放方法。如果您不在乎,可以返回start
和stop
。
但是,您应该将matplotlib.ticker.AutoLocator
(或其他定位器类)子类化,而不是从头开始。
否则,如果您想要自动缩放,则需要重新实现其view_limits
方法。