错误提示:

pyplot.hist()绘制直方图时提示: ValueError : x must have 2 or fewer dimensions

#juzicode.com,#VX:桔子code
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font',family='Youyuan',size='9')
plt.rc('axes',unicode_minus='False')
a = np.random.randn(2000,2,3)
print('a.shape=',a.shape)
plt.hist(a,bins=50)
plt.title('正态分布 by桔子code')
plt.show()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-60-e1963ebdf16f> in <module>
      7 a = np.random.randn(2000,2,3)
      8 print('a.shape=',a.shape)
----> 9 plt.hist(a,bins=50)
     10 plt.title('正态分布 by桔子code')
     11 plt.show()
d:\python\python38\lib\site-packages\matplotlib\pyplot.py in hist(x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, data, **kwargs)
   2683         orientation='vertical', rwidth=None, log=False, color=None,
   2684         label=None, stacked=False, *, data=None, **kwargs):
-> 2685     return gca().hist(
   2686         x, bins=bins, range=range, density=density, weights=weights,
   2687         cumulative=cumulative, bottom=bottom, histtype=histtype,
d:\python\python38\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1445     def inner(ax, *args, data=None, **kwargs):
   1446         if data is None:
-> 1447             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1449         bound = new_sig.bind(ax, *args, **kwargs)
d:\python\python38\lib\site-packages\matplotlib\axes\_axes.py in hist(self, x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
   6570         # Massage 'x' for processing.
-> 6571         x = cbook._reshape_2D(x, 'x')
   6572         nx = len(x)  # number of datasets
d:\python\python38\lib\site-packages\matplotlib\cbook\__init__.py in _reshape_2D(X, name)
   1370             return [np.reshape(x, -1) for x in X]
   1371         else:
-> 1372             raise ValueError(f'{name} must have 2 or fewer dimensions')
   1374     # Iterate over list of iterables.
ValueError: x must have 2 or fewer dimensions

错误原因:

1、pyplot.hist()传入的numpy数组维数不能大于2。

解决方法:

1、a = np.random.randn(2000,2,3)构造的数组是3维的,将第3维拆分,这个例子中第3维的数值为3,可以拆分为3个二维数组:

#juzicode.com,#VX:桔子code
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font',family='Youyuan',size='9')
plt.rc('axes',unicode_minus='False')
a = np.random.randn(2000,2,3)
print('a.shape=',a.shape)
plt.subplots_adjust(wspace=0.2, hspace=0.5)
plt.subplot(221)
plt.title('a[:,:,0]')
plt.hist(a[:,:,0],bins=50) 
plt.subplot(222)
plt.title('a[:,:,1]')
plt.hist(a[:,:,1],bins=50) 
plt.subplot(223)
plt.title('a[:,:,2]')
plt.hist(a[:,:,2],bins=50) 
plt.show()

相关阅读:

数据可视化~matplotlib基本绘图方法
数据可视化~matplotlib显示多个子图 
原文链接:http://www.juzicode.com/archives/3139错误提示:pyplot.hist()绘制直方图时提示:ValueError: x must have 2 or fewer dimensions#juzicode.com,#VX:桔子codeimport numpy as npimport matplotlib.pyplot as pltplt.rc('font',family='Youyuan',size='9')plt.rc('axes',u. 本文主要总结如何 绘制 直方图 ,以及常用的使用场景。 什么是 直方图 :一个随机变量在各个取值区间有个概率分布,将其 绘制 出来:x轴为等间隔的取值区间(bins),y轴为该区间的频数(可归一化),即 直方图 。 Signature: plt . hist ( bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, hist type='bar', align='mid', orientation='vertica
一个简单的 直方图 可以直观地展示数据的分布,包括数值分布的区间、密度和形状。 在实际的工作过程中,我们可能需要对数据进行数学建模和统计分析,这些数据处理技术往往基于数据符合的某些假设,而 直方图 是检查数据最好的选择之一。 下面通过 NumPy 模块提供的随机数据生成函数,产生符合正态分布的随机数据,并以它为样例 绘制 直方图 。 import numpy as np import mat plot lib. py plot as plt randn_data = np.random.randn(1000) plt .his
使用到多条数据,这些数据可分为两个类别,类别集合存放在group_label里,每条数据的类别存放在label里,通过对所有的group_label循环, 绘制 不同类别的数据 直方图 。 fig = plt .figure() for each in group_label: # label为 fig_data = data[[True if i =
这个问题是调用mat plot lib做折线图的 候,输入的x和y的值有问题产生的,因为你画的图。xy的值必须在一个维度(或者说数量不一致),不然就违背了之前的平面图。当然这个 错误 也适用于其他的平面图的 候。 举个例子:关于 错后面就是不一样的了,这个鉴于你x和y值的不同产生的 错误 。 简单说的话就是x的数量和y的数量是要一致的,就不能说我x5个,y6个,这怎么匹对?。 就像这这样 当然其实我犯这个 错误 是因为数据输错了。。。当然,我觉得正常输入数据,应该是很难遇到这个 错误
第一种 : 循环遍历依次遍历图像的像素点,存储不同灰度级的像素点个数 代码(包含画 直方图 ):bool calc Hist ograph(Mat img){ if (!img.data) return false; Mat gray; if (img.channels() == 1) img.co py To(gray);