Created
December 27, 2019 02:53
-
-
Save hadinh1306/4987c1337419dcff35499f5e56becba1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def _visualize_customer_behavior(AccountCode): | |
| """This function visualizes customer behavior using subscription, login and republish events of a customer. | |
| Args: | |
| AccountCode (str): Account unique identification. | |
| Returns: | |
| matplotlib.figure.Figure: a visualization with subscription, login and republish events of a customer. | |
| """ | |
| sample_subscription, sample_republished, sample_login = _get_sample_data(AccountCode) | |
| fig, ax = plt.subplots(figsize=(20, 5)) | |
| # Plot subscription starts and ends | |
| ax.plot(sample_subscription['subscription_starts_at'], sample_subscription['vizline'], | |
| marker='|', linewidth = 0.1, | |
| markersize=50, mew=2, alpha=0.5, | |
| color='royalblue', label='Subscription Starts') | |
| no_expire_mask = ~sample_subscription['subscription_ends_at'].isnull() | |
| ax.plot(sample_subscription[no_expire_mask]['subscription_ends_at'], sample_subscription[no_expire_mask]['vizline'], | |
| linewidth = 0.1, marker='|', | |
| markersize=50, mew=2, alpha=0.5, | |
| color='crimson', label='Subscription Ends') | |
| # Plot login and republish events | |
| ax.plot(sample_login['action_date'], sample_login['vizline'], | |
| marker='o', markersize=11, | |
| alpha=0.3, color='darkseagreen', | |
| linewidth=0.1, label='Login') | |
| ax.plot(sample_republished['action_date'], sample_republished['vizline'], | |
| marker='^', markersize=8, | |
| alpha=0.5, color='teal', | |
| linewidth=0.1, label='Republish') | |
| # Limit date range | |
| datemin = pd.to_datetime('2019/01/01').date() | |
| datemax = pd.to_datetime('2019/12/31').date() | |
| ax.set_xlim(datemin, datemax) | |
| # Show weekly date | |
| date_form = mdates.DateFormatter("%Y/%m/%d") | |
| ax.xaxis.set_major_formatter(date_form) | |
| # Ensure ticks fall once every other week (interval=2) | |
| ax.xaxis.set_major_locator(mdates.WeekdayLocator(interval=2)) | |
| ax.xaxis.set_tick_params(rotation=40) | |
| ax.legend(loc='upper left', ncol=4) | |
| ax.set_title("Customer Behavior") | |
| ax.get_yaxis().set_visible(False) | |
| return fig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment