I have a pandas dataframe that looks like this:
Player_id Draft_Year Rnd Pick Tm ... Vert LeapĀ (in) Broad JumpĀ (in) Shuttle 3Cone 60Yd Shuttle
0 2015Jameis Winston 2015 1 1 TAM ... 28.5 103.0 4.36 7.16 NaN
1 2015Marcus Mariota 2015 1 2 TEN ... 36.0 121.0 4.11 6.87 NaN
2 2015Dante Fowler 2015 1 3 JAX ... 32.5 112.0 4.32 7.40 11.89
3 2015Amari Cooper 2015 1 4 OAK ... 33.0 120.0 3.98 6.71 NaN
4 2015Brandon Scherff 2015 1 5 WAS ... NaN NaN NaN NaN NaN
I'm trying to create a stacked bar plot that will show the distribution of 40 times, color coded by position. The end result will look something like this: Distributuion Bar Plot
I've tried using stacked bar graphs with matplotlib but keep getting an error message. Here is my code:
x = df['40 Yard']
y1 = df['Position Standard'][0]
y2 = df['Position Standard'][1]
y3 = df['Position Standard'][2]
plt.figure(figsize=(4,3))
# stack bars
plt.bar(x, y1, label='y1')
plt.bar(x, y2 ,bottom=y1,label='y2')
plt.bar(x, y3 ,bottom=y1+y2,label='y3')
# memo of sample number
snum = y1+y2+y3
# normalization
y1 = y1/snum*100.
y2 = y2/snum*100.
y3 = y3/snum*100.
# add text annotation corresponding to the percentage of each data.
for xpos, ypos, yval in zip(x, y1/2, y1):
plt.text(xpos, ypos, "%.1f"%yval, ha="center", va="center")
for xpos, ypos, yval in zip(x, y1+y2/2, y2):
plt.text(xpos, ypos, "%.1f"%yval, ha="center", va="center")
for xpos, ypos, yval in zip(x, y1+y2+y3/2, y3):
plt.text(xpos, ypos, "%.1f"%yval, ha="center", va="center")
# add text annotation corresponding to the "total" value of each bar
for xpos, ypos, yval in zip(x, y1+y2+y3, snum):
plt.text(xpos, ypos, "N=%d"%yval, ha="center", va="bottom")
plt.ylim(0,110)
plt.legend(bbox_to_anchor=(1.01,0.5), loc='center left')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-250-521232e1cbd6> in <module>
7 # stack bars
8 plt.bar(x, y1, label='y1')
----> 9 plt.bar(x, y2 ,bottom=y1,label='y2')
10 plt.bar(x, y3 ,bottom=y1+y2,label='y3')
11
~\AppData\Roaming\Python\Python37\site-packages\matplotlib\pyplot.py in bar(x, height, width, bottom, align, data, **kwargs)
2407 return gca().bar(
2408 x, height, width=width, bottom=bottom, align=align,
-> 2409 **({"data": data} if data is not None else {}), **kwargs)
2410
2411
~\AppData\Roaming\Python\Python37\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1563 def inner(ax, *args, data=None, **kwargs):
1564 if data is None:
-> 1565 return func(ax, *map(sanitize_sequence, args), **kwargs)
1566
1567 bound = new_sig.bind(ax, *args, **kwargs)
~\AppData\Roaming\Python\Python37\site-packages\matplotlib\axes\_axes.py in bar(self, x, height, width, bottom, align, **kwargs)
2394 edgecolor=e,
2395 linewidth=lw,
-> 2396 label='_nolegend_',
2397 )
2398 r.update(kwargs)
~\AppData\Roaming\Python\Python37\site-packages\matplotlib\patches.py in __init__(self, xy, width, height, angle, **kwargs)
725 """
726
--> 727 Patch.__init__(self, **kwargs)
728
729 self._x0 = xy[0]
~\AppData\Roaming\Python\Python37\site-packages\matplotlib\patches.py in __init__(self, edgecolor, facecolor, color, linewidth, linestyle, antialiased, hatch, fill, capstyle, joinstyle, **kwargs)
87 self.set_fill(fill)
88 self.set_linestyle(linestyle)
---> 89 self.set_linewidth(linewidth)
90 self.set_antialiased(antialiased)
91 self.set_hatch(hatch)
~\AppData\Roaming\Python\Python37\site-packages\matplotlib\patches.py in set_linewidth(self, w)
393 w = mpl.rcParams['axes.linewidth']
394
--> 395 self._linewidth = float(w)
396 # scale the dash pattern by the linewidth
397 offset, ls = self._us_dashes
TypeError: only size-1 arrays can be converted to Python scalars
Any suggestions?