4.2. Charts
import plotly.graph_objects as go
def plot_AvE(df, factor, min = 0, max = 0, cut_bins = None, y_min = 0, y_max = 1):
df_copy = df.copy()
if cut_bins is not None:
df_copy[factor] = df_copy[factor].astype(float).clip(lower=min, upper=max)
bins = pd.cut(df_copy[factor], bins=cut_bins)
df_copy[factor] = bins.apply(lambda x: x.left)
agg_df = df_copy.groupby(factor).agg({
'RenewalInviteFlag': 'sum',
'RenewalFlag': 'mean',
'base_predictions': 'mean',
'new_predictions': 'mean',
'reg_predictions': 'mean',
'payment_prediction': 'mean'
}).reset_index()
fig = go.Figure()
# Add bar chart trace
fig.add_trace(go.Bar(x=agg_df[factor], y=agg_df['RenewalInviteFlag'], name='Invites', yaxis='y1'))
# Add line chart traces
fig.add_trace(go.Scatter(x=agg_df[factor], y=agg_df['RenewalFlag'], mode='lines+markers', name='Renewals', yaxis='y2'))
fig.add_trace(go.Scatter(x=agg_df[factor], y=agg_df['base_predictions'], mode='lines+markers', name='BasePredictions', yaxis='y2'))
fig.add_trace(go.Scatter(x=agg_df[factor], y=agg_df['new_predictions'], mode='lines+markers', name='NewPredictions', yaxis='y2'))
fig.add_trace(go.Scatter(x=agg_df[factor], y=agg_df['payment_prediction'], mode='lines+markers', name='PayPredictions', yaxis='y2'))
fig.update_layout(
title='Actuals v Base Prediction v New Predictions',
xaxis=dict(title='Category'),
yaxis=dict(
title='Invites',
side='left'
),
yaxis2=dict(
title='Retention Rate',
overlaying='y',
side='right',
range=[y_min, y_max]
),
legend=dict(x=0.99, y=0.01)
)
return fig