如何使用matplotlib制作空直方图?

时间:2013-08-14 20:37:38

标签: python matplotlib histogram

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

>>> 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

如何绘制空直方图?

1 个答案:

答案 0 :(得分:0)

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

if len(data) == 0:
    plt.plot([])
else:
    plt.histogram(data)