I’m looking for a way to graph a histogram for data respecting 2 Dropdown
. I have to choose the value of firstcall
and the value of the secondcall
in order to plot the histogram. I don’t find lot of literature on this subject I hope one of you have already face to this.
Please find an excel file with some data and the code I try bellow:
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')
}
My problem appears here, how can I tell him to take into account 2 conditions (one on first call and one on the second call qualification) ?
@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)
Thanks for your time !
Age_._. : time between calls.
Frequence : should be frequency but it’s really random just to see how it’s working.
Please find the data bellow.