Python 从版本0.99.1.1移动到版本1.2.x时发生matplotlib轴格式错误
在我将matplotlib库从版本“0.99.1.1”升级到版本“1.2.x”后,我不久前编写的一个python脚本突然停止工作 我正在使用Pythonv2.6.5 经过数小时的调试,我终于找到了导致问题的线路。它们是以下两行:Python 从版本0.99.1.1移动到版本1.2.x时发生matplotlib轴格式错误,python,matplotlib,Python,Matplotlib,在我将matplotlib库从版本“0.99.1.1”升级到版本“1.2.x”后,我不久前编写的一个python脚本突然停止工作 我正在使用Pythonv2.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_
# 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
在过去几天绞尽脑汁之后,我已经没有什么想法了。如果您能帮助解决此问题,我们将不胜感激。您的问题是,您没有返回“MajorLocator.view”限制中的任何内容 预计它将实现某种具有视图限制的自动缩放方法。如果你不在乎的话,你可以回到起点和终点 但是,您可能应该子类化或其他定位器类,而不是从头开始 否则,如果希望自动缩放,则需要重新实现其view\u limits方法