gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:38:22 29 4
gpt4 key购买 nike

有人可以帮助我如何在绘图条形图中添加“下拉”菜单。
我在以下链接 ( https://plot.ly/python/v3/dropdowns/ ) 上找到了一些信息,但我正在努力调整代码,因此下拉选项都是特定列中的所有(唯一)值)

例如。 a(我的表的一部分如下:

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

我希望有一个 plotly 条形图,显示每个日期每个“原因”分配的任务数量和一个下拉菜单,我可以在其中选择“名称”列中的名称。

最佳答案

下面的建议会让你选择namereason使用两个下拉菜单。

图 1: Button1=CHRISTOPHE , Button2=ALL
enter image description here

plotly 2: Button1=FERRANTE , Button2=RANDOM
enter image description here

代码:

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

关于python - 基于值列的下拉条形图(绘图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58996541/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com