【发布时间】:2019-07-28 09:09:39
【问题描述】:
我正在寻找一种方法来绘制关于 2 Dropdown 的数据的直方图。我必须选择firstcall 的值和secondcall 的值才能绘制直方图。我没有找到很多关于这个主题的文献我希望你们中的一个人已经面对这个。
请找到一个包含一些数据的 excel 文件和我在下面尝试的代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
df = pd.read_excel(
"/Users/appelexcel.xlsx"
)
mgr_options = df["premierappel"].unique()
mgr_options_second = df["secondappel"].unique()
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
colors = {
'background': '#FDFFFF',
'text': '#0A25DC'
}
app.layout = html.Div(style={'backgroundColor': colors['background']},children=[
html.H1(children='Call',
style={
'textAlign': 'center',
'color': colors['text']
}
),
html.Div(
[
dcc.Dropdown(
id="premierappel",
options=[{
'label': i,
'value': i
} for i in mgr_options],
value='All First Call'),
],
style={'width': '25%',
'display': 'inline-block'}),
dcc.Graph(id='secondcallgraph'),
#The first plot just give the 2nd call
html.Div(
[
dcc.Dropdown(
id="secondappel",
options=[{
'label': i,
'value': i
} for i in mgr_options_second],
value='All Second Call'),
],
style={'width': '25%',
'display': 'inline-block'}),
dcc.Graph(id='thirdcallgraph'), # second figure
])
@app.callback(
dash.dependencies.Output('secondcallgraph', 'figure'),
[dash.dependencies.Input('premierappel', 'value')])
def update_graph(premierappel):
if premierappel == "All First Call":
df_plot = df.copy()
else:
df_plot = df[df['premierappel'] == premierappel]
#func=(lambda x: round(100*x.count()/df_plot.shape[0] ,2))
pv = pd.pivot_table(
df_plot,
index=['Age_1_2'],
columns=['secondappel'],
values=['frequency_1_2'],
aggfunc=sum,
fill_value=0)
trace1 = go.Bar(x=pv.index, y=pv[('frequency_1_2', 'modification')], name='Modification')
trace2 = go.Bar(x=pv.index, y=pv[('frequency_1_2', 'informations')], name='Informations')
trace3 = go.Bar(x=pv.index, y=pv[('frequency_1_2', 'autres')], name='Autres')
trace4 = go.Bar(x=pv.index, y=pv[('frequency_1_2', 'achat')], name='Achat')
return {
'data': [trace1, trace2, trace3, trace4],
'layout':
go.Layout(
title='Appel 2 / {}'.format(premierappel),
xaxis=dict(
title='Days after 1st Call'),
yaxis=dict(
title='Count'),
barmode='stack')
}
第二张图(第三次调用)
我的问题出现在这里,我怎样才能告诉他考虑 2 个条件(一个是第一次通话,一个是第二次通话资格)?
@app.callback(
dash.dependencies.Output('thirdcallgraph', 'figure'),
[dash.dependencies.Input('premierappel', 'value'), dash.dependencies.Input('secondappel', 'value')])
def update_graph(premierappel,secondappel):
if premierappel & secondappel == "All Second Call":
df_plot = df.copy()
else:
df_plot = df[(df['premierappel']==premierappel) & (df['secondappel']==secondappel)]
#func=(lambda x: round(100*x.count()/df_plot.shape[0] ,2))
pv = pd.pivot_table(
df_plot,
index=['Age_2_3'],
columns=['troisiemeappel'],
values=['frequency_2_3'],
aggfunc=sum,
fill_value=0)
trace1 = go.Bar(x=pv.index, y=pv[('frequency_2_3', 'modification')], name='Modification')
trace2 = go.Bar(x=pv.index, y=pv[('frequency_2_3', 'informations')], name='Informations')
trace3 = go.Bar(x=pv.index, y=pv[('frequency_2_3', 'autres')], name='Autres')
trace4 = go.Bar(x=pv.index, y=pv[('frequency_2_3', 'achat')], name='Achat')
return {
'data': [trace1, trace2, trace3, trace4],
'layout':
go.Layout(
title='Appel 2 / {}'.format(secondappel),
xaxis=dict(
title='Days after 2nd Call'),
yaxis=dict(
title='Count'),
barmode='stack')
}
if __name__ == '__main__':
app.run_server(debug=True)
感谢您的宝贵时间!
年龄_._。 : 通话之间的时间。
Frequence :应该是频率,但它确实是随机的,只是为了看看它是如何工作的。
请找到上面的数据。 https://docs.google.com/spreadsheets/d/1u7E6GwJj1nsjOwIQIntcCWCKsczw36_iHPiEV2bjMcs/edit?usp=sharing
【问题讨论】:
标签: python plotly plotly-dash