问题描述
我试图并排获得两个堆叠的条形图,但无法弄清楚.
I am trying to get two stacked bar charts side by side but cannot figure it out.
这是一个df示例:
Field Issue
Police Budget cuts
Research Budget cuts
Police Time consuming
Banking Lack of oversight
Healthcare Lack of support
Research Bureaucracy
Healthcare Bureaucracy
Banking Mistrust
我要的是先堆积的柱状图.它的高度为8,由2个警察,2个研究等分解.然后我要在第一个图表旁边堆积一个发行的条形图.第二个高度为8,并通过2次预算削减,1次耗时,1次缺乏监督等进行叠加.
What I want is a stacked bar chart of of field first. It will have a height of 8 broken down by 2 police, 2 x research etc. Then I want a stacked bar chart of Issue next to the first chart. This second one will have a height of 8 and be stacked by 2 x budget cuts, 1 x time consuming, 1 x lack of oversight etc.
我尝试过:
获取所有字段的堆叠条形图:
to get the stacked bar chart of all fields:
trace1 = go.Bar(
x = df.Field.unique(),
y = df.Field.value_counts(),
name='Total Amount of roles'
)
获取堆积的预算削减条形图(然后复制其他问题):
to get a stacked bar chart of Budget cuts (then replicate for other issues):
trace2 = go.Bar(
x = df.Field.unique(),
y = df[df['Issue'] == 'Budget cuts'].Field.value_counts(),
name='Budget cuts'
)
data = [trace1, trace2]
layout = go.Layout(barmode='stack')
fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename='test.html')
但是上面的代码将两个图堆叠到一个.我要堆叠迹线1和堆叠迹线2.我还希望将其集成到Dash中,而不是单独依靠它,但说实话,这将是次要的.不胜感激!
But the above code stacks the two graphs onto one. I want trace 1 stacked and trace 2 stacked. I also want this integrated into Dash and not plotly on it's own but that would be secondary to be honest. Would appreciate any help!
推荐答案
编辑-在简短的拨号后,这是我的最新建议:
这是一种可能的解决方案,其中每列(字段或问题)堆叠的每个类别的每种出现次数:
Here's a possible solution with the count of each ocurence of each category stacked per column ( Field or Issue):
情节:
代码:
如您所见,它不是非常灵活,因为您必须为每个类别(银行,警察等)添加一个go.Bar
对象.但是,如果上面的图正是您要寻找的内容,我也将对该部分进行整理.
As you can see it's not very flexible since you'll have to add one go.Bar
object for each category (Banking, Police etc). But if the plot above is what you're looking for, I'll sort out that part too.
# import
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
#%qtconsole
# sample data
Field = ['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 indexing
df = 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 / Issue
trace1 = 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 figure
fig = go.Figure(data=data, layout=layout)
# PLot figure
iplot(fig, filename='test.html')
这篇关于Plotly-Dash:要从单个df列中并排放置两个堆叠的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!