python 并列条形图_Plotly-Dash:要从单个df列并排放置两个堆叠的条形图

# importimportpandasaspdimportnumpyasnpimportplotly.graph_objsasgofromplotly.offlineimportdownload_plotlyjs,init_notebook_mode,plot,iplot

init_notebook_mode(connected=True)#%qtconsole# sample dataField=['Police','Research','Police','Banking','Healthcare','Research','Healthcare','Banking']Issue=['Budget cuts','Budget cuts','Time consuming','Lack of oversight','Lack of support','Bureaucracy','Bureaucracy','Mistrust']# Put the lists in a pandas dataframe for# easy grouping and indexingdf=pd.DataFrame([Field,Issue]).T

df.columns=['Field','Issue']grField=df.groupby('Field').count()grIssue=df.groupby('Issue').count()dfgr=pd.concat([grField,grIssue],axis=1,sort=False)dfgr=dfgr.T# Make one go.Bar() object for each category# for corresponing Field / Issuetrace1=go.Bar(x=['Issue'],#y = [dfgr['Field']],y=[dfgr['Banking'].loc['Issue']],name='Banking')trace2=go.Bar(x=['Issue'],#y = [dfgr['Field']],y=[dfgr['Healthcare'].loc['Issue']],name='Healthcare')trace3=go.Bar(x=['Issue'],#y = [dfgr['Field']],y=[dfgr['Police'].loc['Issue']],name='Police')trace4=go.Bar(x=['Issue'],#y = [dfgr['Field']],y=[dfgr['Research'].loc['Issue']],name='Research')trace5=go.Bar(x=['Field'],#y = [dfgr['Field']],y=[dfgr['Budget cuts'].loc['Field']],name='Budget cuts')trace6=go.Bar(x=['Field'],#y = [dfgr['Field']],y=[dfgr['Bureaucracy'].loc['Field']],name='Bureaucracy')trace7=go.Bar(x=['Field'],#y = [dfgr['Field']],y=[dfgr['Lack of oversight'].loc['Field']],name='Lack of oversight')trace7=go.Bar(x=['Field'],#y = [dfgr['Field']],y=[dfgr['Lack of oversight'].loc['Field']],name='Lack of oversight')trace8=go.Bar(x=['Field'],#y = [dfgr['Field']],y=[dfgr['Lack of support'].loc['Field']],name='Lack of support')trace9=go.Bar(x=['Field'],#y = [dfgr['Field']],y=[dfgr['Mistrust'].loc['Field']],name='Mistrust')trace10=go.Bar(x=['Field'],#y = [dfgr['Field']],y=[dfgr['Time consuming'].loc['Field']],name='Time consuming')# gather data and set up layout#data = [trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8, trace9, trace10]data=[trace10,trace9,trace8,trace7,trace6,trace5,trace4,trace3,trace2,trace1]layout=go.Layout(barmode='stack',title='Stacked bar chart from single column')# Build figurefig=go.Figure(data=data,layout=layout)# PLot figureiplot(fig,filename='test.html')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值