Matplotlib条形图中的TypeError

时间:2018-06-28 17:39:10

标签: python python-3.x matplotlib

我是python的新手,正在尝试为图像的RGB值绘制直方图。以下代码给出了TypeError:只能将length-1数组转换为python scaler。

我认为是因为 histr ,因为它是值列表,而不是单个值。

def RGB_hist(image):
    colours = ('r','g','b')
    for i, c in enumerate(colours):
        plt.figure(figsize=(20, 4))
        histr = cv2.calcHist([image], [i], None, [256], [0, 256])

        if c == 'r': colours = [((i/256, 0, 0)) for i in range(0, 256)]
        if c == 'g': colours = [((0, i/256, 0)) for i in range(0, 256)]
        if c == 'b': colours = [((0, 0, i/256)) for i in range(0, 256)]


        plt.bar(range(0, 256), histr, color=colours, edgecolor=colours, width=1)

        plt.show()

RGB_hist(image)

这是我得到的错误:

TypeError                                 Traceback (most recent call last) <ipython-input-28-0646cc857d13> in <module>()  17         plt.show()  18 

---> 19 RGB_hist(image)

<ipython-input-28-0646cc857d13> in RGB_hist(image)
     12         # ERROR IN THIS PART
     13 
---> 14         plt.bar(range(0, 256), histr, color=colours, edgecolor=colours, linewidth=1)
     15         plt.xlim([0, 256])
     16 

~/anaconda3/envs/MLenv/lib/python3.6/site-packages/matplotlib/pyplot.py in bar(*args, **kwargs)    2768                       mplDeprecation)  2769     try:
-> 2770         ret = ax.bar(*args, **kwargs)    2771     finally:    2772         ax._hold = washold

~/anaconda3/envs/MLenv/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)    1853                         "the Matplotlib list!)" % (label_namer, func.__name__),    1854             RuntimeWarning, stacklevel=2)
-> 1855             return func(ax, *args, **kwargs)    1856     1857         inner.__doc__ = _add_data_doc(inner.__doc__,

~/anaconda3/envs/MLenv/lib/python3.6/site-packages/matplotlib/axes/_axes.py in bar(self, *args, **kwargs)    2276                 edgecolor=e,    2277                 linewidth=lw,
-> 2278                 label='_nolegend_',    2279                 )    2280             r.update(kwargs)

~/anaconda3/envs/MLenv/lib/python3.6/site-packages/matplotlib/patches.py in __init__(self, xy, width, height, angle, **kwargs)
    684         """
    685 
--> 686         Patch.__init__(self, **kwargs)
    687 
    688         self._x0 = xy[0]

~/anaconda3/envs/MLenv/lib/python3.6/site-packages/matplotlib/patches.py in __init__(self, edgecolor, facecolor, color, linewidth, linestyle, antialiased, hatch, fill, capstyle, joinstyle, **kwargs)
     93         self.set_fill(fill)
     94         self.set_linestyle(linestyle)
---> 95         self.set_linewidth(linewidth)
     96         self.set_antialiased(antialiased)
     97         self.set_hatch(hatch)

~/anaconda3/envs/MLenv/lib/python3.6/site-packages/matplotlib/patches.py in set_linewidth(self, w)
    360                 w = mpl.rcParams['axes.linewidth']
    361 
--> 362         self._linewidth = float(w)
    363         # scale the dash pattern by the linewidth
    364         offset, ls = self._us_dashes

TypeError: only length-1 arrays can be converted to Python scalars

0 个答案:

没有答案