简体   繁体   English

基于值列的下拉条形图(绘图)

[英]Dropdown bar chart (plotly) based on values column

Can someone help me how to make add a "dropdown" menu in a plotly-bar chart.有人可以帮我如何在绘图条形图中添加“下拉”菜单。 I've found some information on the following link ( https://plot.ly/python/v3/dropdowns/ ) but I'm struggling to adapt the code, so the dropdown options are all (unique) values in a certain column)我在以下链接( https://plot.ly/python/v3/dropdowns/ )上找到了一些信息,但我正在努力调整代码,因此下拉选项是某一列中的所有(唯一)值)

Eg a (Part of my table is the following:例如(我的表的一部分如下:

date        Reason          name            Task
2019-11-17  AI              CHRISTOPHE      3
2019-10-27  RANDOM          CHRISTOPHE      19
2019-11-03  RANDOM          CHRISTOPHE      19
2019-11-10  RANDOM          CHRISTOPHE      49
2019-11-17  RANDOM          CHRISTOPHE      26
2019-10-27  AI              FERRANTE        29
2019-11-03  AI              FERRANTE        40
2019-11-17  AI              FERRANTE        26
2019-10-27  RANDOM          FERRANTE        1
2019-11-03  RANDOM          FERRANTE        4
2019-11-10  RANDOM          FERRANTE        2 
2019-11-17  RANDOM          FERRANTE        2

I would love to have a plotly bar plot showing per date the number of allocated task per "reason" and a dropdown menu where I can select the names in the 'name' col.我希望有一个 plotly 栏 plot 显示每个日期每个“原因”分配的任务数量和一个下拉菜单,我可以在其中 select '名称中的名称。

The following suggestion will let you select name and reason using two drop-down menus.以下建议将让您使用两个下拉菜单 select namereason

Plot 1: Button1=CHRISTOPHE , Button2=ALL Plot 1:按钮 1 Button1=CHRISTOPHE克里斯托弗,按钮 2 Button2=ALL

在此处输入图像描述

Plot 2: Button1=FERRANTE , Button2=RANDOM Plot 2: Button1=FERRANTE FERRANTE, Button2=RANDOM

在此处输入图像描述

Code:代码:

# Imports
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
df = pd.DataFrame({'date': {0: '2019-11-17',
                          1: '2019-10-27',
                          2: '2019-11-03',
                          3: '2019-11-10',
                          4: '2019-11-17',
                          5: '2019-10-27',
                          6: '2019-11-03',
                          7: '2019-11-17',
                          8: '2019-10-27',
                          9: '2019-11-03',
                          10: '2019-11-10',
                          11: '2019-11-17'},
                         'Reason': {0: 'AI',
                          1: 'RANDOM',
                          2: 'RANDOM',
                          3: 'RANDOM',
                          4: 'RANDOM',
                          5: 'AI',
                          6: 'AI',
                          7: 'AI',
                          8: 'RANDOM',
                          9: 'RANDOM',
                          10: 'RANDOM',
                          11: 'RANDOM'},
                         'name': {0: 'CHRISTOPHE',
                          1: 'CHRISTOPHE',
                          2: 'CHRISTOPHE',
                          3: 'CHRISTOPHE',
                          4: 'CHRISTOPHE',
                          5: 'FERRANTE',
                          6: 'FERRANTE',
                          7: 'FERRANTE',
                          8: 'FERRANTE',
                          9: 'FERRANTE',
                          10: 'FERRANTE',
                          11: 'FERRANTE'},
                         'Task': {0: 3,
                          1: 19,
                          2: 19,
                          3: 49,
                          4: 26,
                          5: 29,
                          6: 40,
                          7: 26,
                          8: 1,
                          9: 4,
                          10: 2,
                          11: 2}})

# split df by names
names = df['name'].unique().tolist()
dates = df['date'].unique().tolist()

dfs = {}

# dataframe collection grouped by names
for name in names:
    #print(name)
    dfs[name]=pd.pivot_table(df[df['name']==name],
                             values='Task',
                             index=['date'],
                             columns=['Reason'],
                             aggfunc=np.sum)

# plotly start 
fig = go.Figure()

# get column names from first dataframe in the dict
colNames = list(dfs[list(dfs.keys())[0]].columns)
#xValues=

# one trace for each column per dataframe: AI and RANDOM
for col in colNames:
    fig.add_trace(go.Bar(x=dates,
                             visible=True,
                             #name=col
                  )
             )

# menu setup    
updatemenu= []

# buttons for menu 1, names
buttons=[]

# create traces for each Reason: AI or RANDOM
for df in dfs.keys():
    buttons.append(dict(method='update',
                        label=df,
                        visible=True,
                        args=[#{'visible':True},
                              #{'x':[dfs[df]['AI'].index, dfs[df]['RANDOM'].index]},
                              {'y':[dfs[df]['AI'].values, dfs[df]['RANDOM'].values]}])
                  )

# buttons for menu 2, reasons
b2_labels = colNames

# matrix too feed all visible arguments for all traces
# so that they can be shown or hidden by choice
b2_show = [list(b) for b in [e==1 for e in np.eye(len(b2_labels))]]
buttons2=[]
buttons2.append({'method': 'update',
                 'label': 'All',
                 'args': [{'visible': [True]*4}]})

# create buttons to show or hide
for i in range(0, len(b2_labels)):
    buttons2.append(dict(method='update',
                        label=b2_labels[i],
                        args=[{'visible':b2_show[i]}]
                        )
                   )

# add option for button two to hide all
buttons2.append(dict(method='update',
                        label='None',
                        args=[{'visible':[False]*4}]
                        )
                   )

# some adjustments to the updatemenus
updatemenu=[]
your_menu=dict()
updatemenu.append(your_menu)
your_menu2=dict()
updatemenu.append(your_menu2)
updatemenu[1]
updatemenu[0]['buttons']=buttons
updatemenu[0]['direction']='down'
updatemenu[0]['showactive']=True
updatemenu[1]['buttons']=buttons2
updatemenu[1]['y']=0.6

fig.update_layout(showlegend=False, updatemenus=updatemenu)
fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM