1

我正在绘制直方图,然后将它们拼接在一起以制作动画图。有时我的数据确实是一个空列表,但出现以下代码错误:

>>> plt.hist([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 2008, in hist
    ret = ax.hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, **kwargs)
  File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 7116, in hist
    m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
  File "/usr/lib64/python2.6/site-packages/numpy/lib/function_base.py", line 202, in histogram
    range = (a.min(), a.max())
ValueError: zero-size array to ufunc.reduce without identity

如何绘制空直方图?

4

1 回答 1

0

空直方图看起来与空图相同,并且 plot 命令可以采用空列表。

if len(data) == 0:
    plt.plot([])
else:
    plt.histogram(data)
于 2013-08-14T21:25:17.190 回答