【问题标题】:Matplot data visualization - height argument must be scalarMatplotlib 数据可视化 - 高度参数必须是标量
【发布时间】: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


【解决方案1】:

所以,根据https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.bar.html

第二个参数必须是高度

您输入 n 作为第二个参数,它是一个数字

试试

r1 = ax.bar(index, missing_count, 0.15, color='r')

相反,这应该可以完成工作。

更好的是,明确你的参数名称(乏味,更难保持清洁,但当你有多个参数时,这是一个好主意)

r1 = ax.bar(x=index, height = missing_count, width = 0.15, color='r')

第二个参数必须是高度;高度对应于任何特定框的计数。假设你有一个由 0 和 1 组成的数组

A = [0,0,0,0,1,1,1]

这将导致一个带有两个条形的条形图,一个是 4 个单位高(因为你有四个零),另一个是 3 个单位高

命令

r1 = ax.bar([0,1], [4,3], 0.15, color='r')

将绘制一个图,其中一个条位于 0,一个条位于 1。第一个条高 4 个单位,第二个条高 3 个单位。

翻译成你的代码,missing_count对应数组的COUNT 那不是A,而是[Counter([0,0,0,0,1,1,1])[x] for x in Counter([0,0,0,0,1,1,1])]

【讨论】:

  • 这似乎可行,谢谢。但是条形图的高度怎么可能必须以 nparray 的形式呈现呢?这个论点现在是否兼作使用的数据和高度?
【解决方案2】:

在代码中n 是标量。您可能不希望条形高度保持不变,而是希望 missing_count 中的值保持不变。

ax.bar(index, missing_count, 0.15, color='r')

【讨论】:

    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 2011-07-04
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    相关资源
    最近更新 更多