尝试绘图,我从matplotlib
收到了以下错误:
TypeError: %d format: a number is required, not numpy.float64
这是完整的追溯(我修改了路径名):
Traceback (most recent call last):
File ".../plotmod.py", line 154, in _plot
fig.autofmt_xdate()
File ".../local/lib/python2.7/site-packages/matplotlib/figure.py", line 426, in autofmt_xdate
for label in ax.get_xticklabels():
File ".../local/lib/python2.7/site-packages/matplotlib/axes.py", line 2620, in get_xticklabels
self.xaxis.get_ticklabels(minor=minor))
File ".../local/lib/python2.7/site-packages/matplotlib/axis.py", line 1118, in get_ticklabels
return self.get_majorticklabels()
File ".../local/lib/python2.7/site-packages/matplotlib/axis.py", line 1102, in get_majorticklabels
ticks = self.get_major_ticks()
File ".../local/lib/python2.7/site-packages/matplotlib/axis.py", line 1201, in get_major_ticks
numticks = len(self.get_major_locator()())
File ".../local/lib/python2.7/site-packages/matplotlib/dates.py", line 595, in __call__
'RRuleLocator estimated to generate %d ticks from %s to %s: exceeds Locator.MAXTICKS * 2 (%d) ' % (estimate, dmin, dmax, self.MAXTICKS * 2))
TypeError: %d format: a number is required, not numpy.float64
这个错误来自哪里?
我做了一些基础研究结果:
RuntimeError
提出的matplotlib.dates
消息时导致的错误%d
,它似乎无法处理numpy.float64
个实例estimate
,这是matplotlib
的内部计算结果,或MAXTICKS
,这可能是一个常数,因此我倾向于相信这是第一个选择estimate
涉及date2num
应该返回合法值,而_get_unit()
和_get_interval()
则深入到模块中,这就是我的研究停止。我可以轻松地在我的整个软件框架中重现错误,但我无法将其隔离以便于再现代码。我认为当应该绘制的整个轴非常短(例如,长达几分钟)时,往往会发生这种情况。
有什么想法吗?
答案 0 :(得分:2)
你似乎有一个NaN或无穷大,你试图格式化为一个引发错误的整数(对于int数据类型,没有NaN或Inf这样的东西)。
In [1]: import numpy as np
In [2]: '%d' % np.float64(42)
Out[2]: '42'
In [3]: '%d' % np.float64('nan')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython console> in <module>()
TypeError: %d format: a number is required, not numpy.float64
In [4]: '%d' % np.float64('inf')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
ipython console> in <module>()
TypeError: %d format: a number is required, not numpy.float64
您可以进入matplotlib文件(或使用python调试器)生成错误并将打印行更改为%f
,这将适用于所有numpy浮点数。 ('%f' % np.float64('nan')
返回'nan'
)。
答案 1 :(得分:0)
手动将numpy float转换为Python float。
np.asscalar(np.float64(42))