Matplotlib的RGB直方图条中的类型错误

时间:2018-10-14 05:32:25

标签: python matplotlib

当我尝试使用以下代码绘制histogram bar plot时。

def show_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])
#         plt.plot(histr, color=c, lw=2)
        print(histr)

        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.xlim([0, 256])

        plt.show()

show_rgb_hist(image)

我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-f1c3e87fb229> in <module>()
     22         plt.show()
     23 
---> 24 show_rgb_hist(image)

<ipython-input-49-f1c3e87fb229> in show_rgb_hist(image)
     17 
     18 
---> 19         plt.bar(range(0, 256), histr, color=colours, edgecolor=colours, width=1)
     20 #         plt.xlim([0, 256])
     21 

~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in bar(*args, **kwargs)
   2646                       mplDeprecation)
   2647     try:
-> 2648         ret = ax.bar(*args, **kwargs)
   2649     finally:
   2650         ax._hold = washold

~/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1715                     warnings.warn(msg % (label_namer, func.__name__),
   1716                                   RuntimeWarning, stacklevel=2)
-> 1717             return func(ax, *args, **kwargs)
   1718         pre_doc = inner.__doc__
   1719         if pre_doc is None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in bar(self, *args, **kwargs)
   2083                 edgecolor=e,
   2084                 linewidth=lw,
-> 2085                 label='_nolegend_',
   2086                 )
   2087             r.update(kwargs)

~/anaconda3/lib/python3.6/site-packages/matplotlib/patches.py in __init__(self, xy, width, height, angle, **kwargs)
    657         """
    658 
--> 659         Patch.__init__(self, **kwargs)
    660 
    661         self._x = xy[0]

~/anaconda3/lib/python3.6/site-packages/matplotlib/patches.py in __init__(self, edgecolor, facecolor, color, linewidth, linestyle, antialiased, hatch, fill, capstyle, joinstyle, **kwargs)
    101         self.set_fill(fill)
    102         self.set_linestyle(linestyle)
--> 103         self.set_linewidth(linewidth)
    104         self.set_antialiased(antialiased)
    105         self.set_hatch(hatch)

~/anaconda3/lib/python3.6/site-packages/matplotlib/patches.py in set_linewidth(self, w)
    349                 w = mpl.rcParams['axes.linewidth']
    350 
--> 351         self._linewidth = float(w)
    352         # scale the dash pattern by the linewidth
    353         offset, ls = self._us_dashes

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

这里histr

[[0.000e+00]
 [0.000e+00]
 [0.000e+00]
 [0.000e+00]
 [0.000e+00]
 [0.000e+00]
 [0.000e+00]
 [0.000e+00]
 [1.000e+00]
 [1.700e+01]
 [8.700e+01]
 [2.620e+02]
 [2.860e+02]
 [3.000e+02]
 [4.220e+02]
 [6.880e+02]
 [1.323e+03]
 [1.921e+03]
 [1.929e+03]
 [1.527e+03]
 [9.850e+02]
 [6.630e+02]
 [4.850e+02]
 [4.710e+02]
 [4.550e+02]
 [4.300e+02]
 [4.520e+02]
 [4.310e+02]
 [4.660e+02]
 [5.460e+02]
 [5.910e+02]
 [6.080e+02]
 [6.660e+02]
 [6.330e+02]
 [6.280e+02]
 [6.890e+02]
 [7.380e+02]
 [6.410e+02]
 [6.150e+02]
 [6.340e+02]
 [6.130e+02]
 [6.320e+02]
 [6.660e+02]
 [6.430e+02]
 [6.310e+02]
 [6.280e+02]
 [6.760e+02]
 [6.590e+02]
 [6.380e+02]
 [6.830e+02]
 [6.170e+02]
 [6.550e+02]
 [6.690e+02]
 [6.960e+02]
 [7.230e+02]
 [6.980e+02]
 [8.560e+02]
 [8.600e+02]
 [8.500e+02]
 [9.230e+02]
 [9.250e+02]
 [9.680e+02]
 [1.069e+03]
 [1.232e+03]
 [1.333e+03]
 [1.279e+03]
 [1.378e+03]
 [1.455e+03]
 [1.527e+03]
 [1.724e+03]
 [1.986e+03]
 [2.080e+03]
 [2.363e+03]
 [2.468e+03]
 [2.535e+03]
 [2.565e+03]
 [2.572e+03]
 [2.552e+03]
 [2.399e+03]
 [2.493e+03]
 [2.562e+03]
 [2.506e+03]
 [2.461e+03]
 [2.497e+03]
 [2.466e+03]
 [2.532e+03]
 [2.590e+03]
 [2.684e+03]
 [2.772e+03]
 [2.817e+03]
 [2.865e+03]
 [2.735e+03]
 [2.708e+03]
 [2.903e+03]
 [3.105e+03]
 [3.042e+03]
 [3.151e+03]
 [3.011e+03]
 [3.022e+03]
 [3.288e+03]
 [3.584e+03]
 [3.727e+03]
 [4.296e+03]
 [4.602e+03]
 [4.926e+03]
 [4.898e+03]
 [4.506e+03]
 [4.410e+03]
 [4.288e+03]
 [4.148e+03]
 [4.090e+03]
 [3.954e+03]
 [4.042e+03]
 [4.204e+03]
 [4.346e+03]
 [4.328e+03]
 [4.077e+03]
 [3.932e+03]
 [3.898e+03]
 [3.877e+03]
 [3.543e+03]
 [3.477e+03]
 [3.380e+03]
 [3.459e+03]
 [3.354e+03]
 [3.462e+03]
 [3.388e+03]
 [3.332e+03]
 [3.122e+03]
 [3.001e+03]
 [3.021e+03]
 [2.900e+03]
 [2.987e+03]
 [3.054e+03]
 [3.027e+03]
 [2.735e+03]
 [2.396e+03]
 [2.071e+03]
 [1.848e+03]
 [1.870e+03]
 [1.679e+03]
 [1.720e+03]
 [1.581e+03]
 [1.532e+03]
 [1.407e+03]
 [1.390e+03]
 [1.358e+03]
 [1.256e+03]
 [1.237e+03]
 [1.096e+03]
 [1.096e+03]
 [9.560e+02]
 [8.970e+02]
 [8.420e+02]
 [8.500e+02]
 [8.550e+02]
 [8.450e+02]
 [8.040e+02]
 [8.130e+02]
 [7.530e+02]
 [7.760e+02]
 [7.700e+02]
 [7.430e+02]
 [7.400e+02]
 [7.350e+02]
 [7.190e+02]
 [7.630e+02]
 [7.400e+02]
 [7.850e+02]
 [7.750e+02]
 [7.830e+02]
 [7.090e+02]
 [7.820e+02]
 [7.890e+02]
 [7.860e+02]
 [7.200e+02]
 [7.060e+02]
 [6.510e+02]
 [6.820e+02]
 [6.460e+02]
 [5.930e+02]
 [5.920e+02]
 [4.670e+02]
 [5.120e+02]
 [4.820e+02]
 [4.750e+02]
 [5.010e+02]
 [5.040e+02]
 [4.970e+02]
 [4.380e+02]
 [5.090e+02]
 [4.830e+02]
 [4.660e+02]
 [4.240e+02]
 [4.870e+02]
 [4.770e+02]
 [4.860e+02]
 [4.960e+02]
 [4.720e+02]
 [4.870e+02]
 [4.440e+02]
 [4.600e+02]
 [4.480e+02]
 [4.250e+02]
 [4.270e+02]
 [3.850e+02]
 [3.700e+02]
 [4.120e+02]
 [3.860e+02]
 [3.860e+02]
 [3.890e+02]
 [3.650e+02]
 [3.680e+02]
 [3.570e+02]
 [3.490e+02]
 [3.600e+02]
 [3.620e+02]
 [3.070e+02]
 [3.410e+02]
 [3.410e+02]
 [3.270e+02]
 [3.120e+02]
 [3.320e+02]
 [2.940e+02]
 [2.780e+02]
 [2.750e+02]
 [2.790e+02]
 [2.660e+02]
 [2.960e+02]
 [2.570e+02]
 [2.780e+02]
 [2.630e+02]
 [2.820e+02]
 [2.830e+02]
 [2.220e+02]
 [1.820e+02]
 [1.780e+02]
 [2.020e+02]
 [1.620e+02]
 [1.840e+02]
 [1.500e+02]
 [1.620e+02]
 [1.360e+02]
 [1.050e+02]
 [1.060e+02]
 [1.170e+02]
 [9.400e+01]
 [7.300e+01]
 [6.900e+01]
 [6.400e+01]
 [7.000e+01]
 [6.100e+01]
 [5.500e+01]
 [6.100e+01]
 [8.000e+01]
 [3.590e+02]]

NB:

cv2.__version__
'3.4.1'

我该如何解决这个问题?

0 个答案:

没有答案