如何在Python中创建分布条形图

问题描述

我有一个看起来像这样的熊猫数据框:

    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

我正在尝试创建一个堆叠的条形图,该条形图将显示40次分布,并按位置进行颜色编码。最终结果将如下所示: Distributuion Bar Plot

我尝试将堆叠的条形图与matplotlib结合使用,但始终收到错误消息。这是我的代码

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,"%.1f"%yval,ha="center",va="center")
for xpos,y1+y2/2,y2):
    plt.text(xpos,y1+y2+y3/2,y3):
    plt.text(xpos,va="center")
# add text annotation corresponding to the "total" value of each bar
for xpos,y1+y2+y3,snum):
    plt.text(xpos,"N=%d"%yval,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,label='y1')
----> 9 plt.bar(x,label='y2')
     10 plt.bar(x,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,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,*args,**kwargs)
   1563     def inner(ax,data=None,**kwargs):
   1564         if data is None:
-> 1565             return func(ax,*map(sanitize_sequence,args),**kwargs)
   1566 
   1567         bound = new_sig.bind(ax,**kwargs)

~\AppData\Roaming\Python\python37\site-packages\matplotlib\axes\_axes.py in bar(self,x,**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,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

有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)