我想显示一个比较 y 和 yHat( 两个数组各保存三个值的条形图 (,尽管使用了矢量化和 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'
我忘记了arrrays不是1-D的,我所要做的就是使用.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)'])