【发布时间】: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