Matplotlib 不显示我的图形(使用 vim)

Matplotlib doesn't show my graph (using vim)

我在使用 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。它现在不会影响你,但以后可能会困扰你。