Python Plotly Dash:希望从单个df列中并排显示两个堆叠条形图

Python Plotly Dash:希望从单个df列中并排显示两个堆叠条形图,python,python-3.x,plotly,Python,Python 3.x,Plotly,我试图得到两个堆叠的条形图并排,但无法找出它 下面是一个示例: Field Issue Police Budget cuts Research Budget cuts Police Time consuming Banking Lack of oversight Healthcare Lack of support Research Bureaucracy Healthcare Bureaucracy Banking Mistru

我试图得到两个堆叠的条形图并排,但无法找出它

下面是一个示例:

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
我想要的是一个堆叠的条形图领域第一。它将有一个由2名警察,2 x研究等分解的高度为8。然后我想在第一个图表旁边有一个问题的堆积条形图。这第二个将有8个高度,并由2倍的预算削减,1倍的时间消耗,1倍缺乏监督等叠加

我试过:

要获取所有字段的堆叠条形图,请执行以下操作:

trace1 = go.Bar(
    x = df.Field.unique(),
    y = df.Field.value_counts(),
    name='Total Amount of roles'
)
要获得预算削减的堆叠条形图(然后复制其他问题):


但是上面的代码将两个图叠加到一个图上。我要堆叠跟踪1和跟踪2。我也希望将其整合到Dash中,而不是单独的,但老实说,这是次要的。非常感谢您的帮助

编辑-在评论中打了一个简短的电话后,这是我的最新建议:


下面是一个可能的解决方案,其中每列(字段或问题)中每个类别的每个文档的计数:

绘图:

代码:

正如您所见,它不是很灵活,因为您必须为每个类别(银行、警察等)添加一个
go.Bar
对象。但是如果上面的情节是你要找的,我也会把那部分整理出来

# 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')

工作代码片段,如果您需要:

import plotly.graph_objects as go

x=['a','b','c','d']
fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal',
                   base = 0, width = 0.2, offset = 0.0,
                   marker = dict(color = 'rgb(0,120,255)')))

fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa',
                width = 0.2, offset = -0.2,
                marker = dict(color = 'rgb(250,60,0)')))
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto',
                 width = 0.2, offset = -0.2,
                 marker = dict(color = 'rgb(250,130,0)')))

fig.update_layout(barmode='stack', xaxis={'categoryorder':'array', 'categoryarray':['d','a','c','b']})
fig.show()
另一种布局:
更改:第二个图形的基准偏移量

import plotly.graph_objects as go

x=['a','b','c','d']
fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal',
                   base = 0, width = 0.2, offset = 0.0,
                   marker = dict(color = 'rgb(0,120,255)')))

fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa',
                width = 0.2, offset = -0.4, base=0,
                marker = dict(color = 'rgb(250,60,0)')))
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto',
                 width = 0.2, offset = -0.2,
                 marker = dict(color = 'rgb(250,130,0)')))

fig.update_layout(barmode='stack', xaxis={'categoryorder':'array', 'categoryarray':['d','a','c','b']})
fig.show()

通过
pyo.plot(图,filename='test.html')
您一定是指
py.plot(图,filename='test.html')
?这当然取决于你的进口产品是什么样子的,但我还是改变了它。嗨,维斯特兰,谢谢你的代码。这张图就是我现在看到的,我就是这样做的。我要找的基本上是一个堆叠条形图,显示有2个警察、2个研究等,高度为8(因为有8个字段),第二个堆叠条形图显示的问题高度为8(按2的高度堆叠用于预算削减,一个用于耗时等)。一个堆叠图表用于显示字段数的明细,另一个堆叠图表用于显示问题数的明细。这有意义吗?Thanks@user8322222那么,两个酒吧?在这种情况下,这里的挑战是数据转换,而不是绘图本身,这才是挑战。我来看看。是的,一共两条。谢谢维斯特兰,没问题,但我们还没到。明天我再看一看。@user8322222很乐意帮忙!如果我的回答回答了你的问题,请考虑把它标记为被接受的答案。别担心,我会找时间让它更灵活,因为这对我来说也很有趣。任何其他细节都可以在已建立的聊天室中讨论。
import plotly.graph_objects as go

x=['a','b','c','d']
fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal',
                   base = 0, width = 0.2, offset = 0.0,
                   marker = dict(color = 'rgb(0,120,255)')))

fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa',
                width = 0.2, offset = -0.4, base=0,
                marker = dict(color = 'rgb(250,60,0)')))
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto',
                 width = 0.2, offset = -0.2,
                 marker = dict(color = 'rgb(250,130,0)')))

fig.update_layout(barmode='stack', xaxis={'categoryorder':'array', 'categoryarray':['d','a','c','b']})
fig.show()