问题描述
我在使用 matplotlib 时遇到了问题。我正在使用 linux Chrome OS 发行版。当我尝试创建图表时,它没有显示任何内容,只是这个: Graph
这是脚本:
import matplotlib.pyplot as plt
x = [10,20,30,40,50,60]
bin = [10]
plt.hist(x,bin)
plt.show()
我的 IDE 是 Vim
解决方法
发生这种情况是因为您向 plt.hist
提供了错误的参数。根据文档:
bins : int or sequence or str,default: :rc:`hist.bins`
If *bins* is an integer,it defines the number of equal-width bins
in the range.
If *bins* is a sequence,it defines the bin edges,including the
left edge of the first bin and the right edge of the last bin;
in this case,bins may be unequally spaced. All but the last
(righthand-most) bin is half-open. In other words,if *bins* is::
[1,2,3,4]
then the first bin is ``[1,2)`` (including 1,but excluding 2) and
the second ``[2,3)``. The last bin,however,is ``[3,4]``,which
*includes* 4.
If *bins* is a string,it is one of the binning strategies
supported by `numpy.histogram_bin_edges`: 'auto','fd','doane','scott','stone','rice','sturges',or 'sqrt'.
因此,通过提供 [10]
,您没有提供 bin 的结尾,只提供了开头,并且没有生成任何内容。您可以使用 plt.hist(x,10)
或 plt.hist(x,[10,20,30 <rest>])
解决此问题。
此外,尽量避免覆盖 bin
等内置函数。现在不会影响你,但以后可能会困扰你。