首页 > 解决方案 > Plotly-Dash 堆叠条形图并排响应下拉

问题描述

我有以下数据框:

-- 用于选择问题的 dcc.Dropdown 位于页面上 --

Job  Tenure  Location  Topic
CSM   3-5    England   Budget cuts 
CSM   6-10   Scotland  Budget cuts
SA    0-2    India     Time consuming demands
SA    3-5    England   Lack of oversight
SA    6-10   Germany   Lack of support
MIA   11-15  India     Lack of support
ADCS  20+    England   Bureaucracy
MIA   16-20  Ireland   Bureaucracy
ADCS  20+    USA       Budget cuts

最终结果需要有三组图表。每组由两个并排堆叠的条形图组成。

结果如下:

图一由两个并排的条形图组成:

条 1 = 堆叠作业(高度 = 9,堆叠由 2xCSM、3xSA、2xMIA、2xADCS 组成)

bar2 = 按角色划分的问题。例如,如果在下拉列表中选择了问题“预算削减”,则此图的高度 = 3,堆栈由 2xCSM、1xADCS 组成。

当您从下拉列表中选择问题时,栏 2 需要更新。因此,如果我现在选择问题“缺乏监督”,则条形 2 的高度将为 1,按作业 1 x SA 堆叠。

图 2 和图 3 将与上面相同,但分别是任期和位置而不是工作。因此,在上述实例中为 Job 的所有实例中,堆栈将是 Tenure/Location。

老实说,即使有人可以告诉我如何制作第一个图表,我也可以复制图表 2 和 3 的代码。我希望这是有道理的。

这是我得到的输出。我基本上希望同一轴上的这两个条不要像这里那样分开(忽略高度值,因为我的 df 大于我在这里给出的值)。

在此处输入图像描述

多谢你们。

标签: pythonpython-3.xplotlyplotly-dash

解决方案


这是一个简单的工作示例,改编自

import dash
import dash_core_components as dcc
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
                {'x': [1, 2, 3], 'y': [5, 2, 6], 'type': 'bar', 'name': 'SF', 'xaxis': 'x2',},
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': u'Montréal', 'yaxis': 'y2',},
            ],
            'layout': {
                'title': 'Dash Data Visualization',
                'xaxis': {'domain':[0, 0.5]},
                'xaxis2': {'domain':[0.6, 1]},
                'yaxis2': {'anchor': 'x2'}
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

它应该输出类似的东西,

在此处输入图像描述


推荐阅读