Gargee Suresh
Gargee Suresh

Reputation: 319

How to remove TypeError: unsupported operand type(s) for +: 'int' and 'vectorize' , vectorize, astype not working

I want to display a bar graph comparing y and yHat( both arrays holding three values each ), It's showing errors despite using vectorize and 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'

Upvotes: 0

Views: 1931

Answers (1)

Gargee Suresh
Gargee Suresh

Reputation: 319

I forgot that the arrrays weren't 1-D, all I had to do was flatten them using .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)'])

Upvotes: 1

Related Questions