【发布时间】:2018-04-23 17:48:16
【问题描述】:
我一直在尝试使用 Python 和 Matplot 进行一些数据可视化。在这种情况下,我试图可视化每列丢失的数据量。我运行了一个简短的脚本来查找每列的所有缺失值以及数组 missing_count 中的结果。我现在想使用 Matplot 在条形图中显示它,但我遇到了这个问题:
import matplotlib.pyplot as plt
import numpy as np
missing_count = np.array([33597, 0, 0, 0, 0, 0, 0, 12349, 0, 0, 12349, 0, 0, 0, 115946, 47696, 44069, 81604, 5416, 5416, 5416, 5416, 0, 73641, 74331, 187204, 128829, 184118, 116441, 183093, 153048, 187349, 89918, 89918, 89918, 89918, 89918, 89918, 51096, 51096, 51096, 51096, 51096, 51096, 51096, 51096, 51096, 51096])
n = len(missing_count)
index = np.arange(n)
fig, ax = plt.subplots()
r1 = ax.bar(index, n, 0.15, missing_count, color='r')
ax.set_ylabel('NULL values')
ax.set_title('Amount of NULL values per colum')
ax.set_xticks(index + width / 2)
ax.set_xticklabels(list(originalData.columns.values))
plt.show()
导致这个错误:
ValueError Traceback (most recent call last)
<ipython-input-34-285ca1e9de68> in <module>()
10 fig, ax = plt.subplots()
11
---> 12 r1 = ax.bar(index, n, 0.15, missing_count, color='r')
13
14 ax.set_ylabel('NULL values')
C:\Users\Martien\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
1895 warnings.warn(msg % (label_namer, func.__name__),
1896 RuntimeWarning, stacklevel=2)
-> 1897 return func(ax, *args, **kwargs)
1898 pre_doc = inner.__doc__
1899 if pre_doc is None:
C:\Users\Martien\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in bar(self, left, height, width, bottom, **kwargs)
2077 if len(height) != nbars:
2078 raise ValueError("incompatible sizes: argument 'height' "
-> 2079 "must be length %d or scalar" % nbars)
2080 if len(width) != nbars:
2081 raise ValueError("incompatible sizes: argument 'width' "
ValueError: incompatible sizes: argument 'height' must be length 48 or scalar
我查看了 Matplot 文档,它告诉我高度应该是一个标量,但它没有引用或解释这个标量是什么。还有一个this 示例我已经遵循,当我运行它时它确实有效。
对于为什么会出现此错误,我已经没有想法了,非常感谢所有帮助。
编辑:originalData 是我读入的原始 CSV 文件,我在这里只用它来命名我的酒吧
【问题讨论】:
-
尝试 type(index) 会得到什么?
-
所以条的高度必须是该列中有多少个0?还是您只是想绘制
missing_count的条形图? -
@DavidG ;missing_count' 的图表,我收集了该数组中每列 NULL 值、0 和 99999 的数量
-
@MohammadAthar 索引是一个 numpy.ndarray
标签: python matplotlib