【问题标题】:How to remove TypeError: unsupported operand type(s) for +: 'int' and 'vectorize' , vectorize, astype not working如何删除 TypeError:+ 的不支持的操作数类型:“int”和“vectorize”,vectorize,astype 不起作用
【发布时间】:2020-04-09 20:42:39
【问题描述】:

我想显示一个比较 y 和 yHat( 两个数组每个都包含三个值的条形图 ),尽管使用了 vectorize 和 astype(float),它仍然显示错误

plt.bar([0.35,1.35,2.35],np.vectorize(yHat.astype(float)), width = 0.35, color='r', alpha=0.8)
plt.grid(True)
plt.legend(['y(expected)','yHat(recieved)'])
'''
TypeError  Traceback (most recent call last)
<ipython-input-59-343a5f511526> in <module>()
----> 1 plt.bar([0.00,1.00,2.00], np.vectorize(y.astype(float)), width = 0.35, alpha=0.8)
      2 plt.bar([0.35,1.35,2.35],np.vectorize(yHat.astype(float)), width = 0.35, color='r', alpha=0.8)
      3 plt.grid(True)
      4 plt.legend(['y(expected)','yHat(recieved)'])

3 frames
/usr/local/lib/python3.6/dist-packages/matplotlib/patches.py in __init__(self, xy, width, height, angle, **kwargs)
    714 
    715         self._x1 = self._x0 + self._width
--> 716         self._y1 = self._y0 + self._height
    717 
    718         self.angle = float(angle)

TypeError: unsupported operand type(s) for +: 'int' and 'vectorize'

【问题讨论】:

  • 你不应该向量化,而是使用数组原样
  • 这就是我最初的做法,它显示 TypeError: only size-1 arrays can be convert to Python scalars
  • 然后使您的数组大小相同,而不是矢量化
  • 它们的大小相同。
  • then sth else is wrong,再次vectorize与此无关。 here is the matplotlib doc for bar charts

标签: python numpy matplotlib graph jupyter-notebook


【解决方案1】:

我忘记了数组不是一维的,我所要做的就是使用 .flatten() 将它们展平

    plt.bar(([0.00,1.00,2.00]), (y.flatten()), width = 0.35,  bottom=None,align='edge', color='r',data=None)
    plt.bar(([0.35,1.35,2.35]),(yHat.flatten()), width = 0.35, bottom=None,align='edge', color='b',data=None)
    plt.grid(True)
    plt.legend(['y(expected)','yHat(recieved)'])

【讨论】:

    猜你喜欢
    • 2017-12-27
    • 2014-03-31
    • 2012-12-12
    • 2020-02-27
    • 2021-05-23
    • 2016-04-15
    • 2019-01-12
    • 2012-11-29
    • 2012-12-31
    相关资源
    最近更新 更多