4 Tips to Visualize Behavior With Python Matplotlib, Follow By Easy Tutorial
By: Ha Dinh
Tags: Programming, Data Science, Saas Marketing, Data Visualization, Customer Behavior

4 Tips to Visualize Behavior With Python Matplotlib, Follow By Easy Tutorial

Tips to work with different action events, and events that happen multiple times

Why Visualizing Customer Behavior?

I’m currently working for Unbounce as a data analyst. Unbounce is a subscription-based tool that helps marketers build and optimize landing pages for a high conversion rate. Like any other subscription-based companies, we want to reduce churn and attract customers to stay longer. A big part of it is to understand which behaviors correlate to longer customer retention, why so, and how we can nurture those behaviors.

Although there are many statistical methods we can use to check customer behavior trends, I find it helpful to visualize the behavior of some customers (and discuss with customer-facing teams) to form hypotheses that can be validated later on.

The Data

For this tutorial, we will use sample data that includes events and subscription information for 4 accounts. No personal information is included, and account unique identifications have been changed to ensure security.

Customer behavior data usually includes date and time events, the moments when customers do a particular action. In this tutorial, we will look into events for account republish ( republished_df ) and login ( login_df ).

We also have subscription information for each customer ( subscription_info_df ). A customer can have multiple subscriptions, but each subscription is mutually exclusive. A new subscription for a customer only starts when he/she churns (meaning stop paying) then re-subscribe. We call this person a flapper.

Load and Transform

republished_df = pd.read_csv(filepath)

login_df = pd.read_csv(filepath)

subscription_info_df = pd.read_csv(filepath)

Before going into the visualization, we need to transform date columns to date-time format. Right now, Python thinks that they are a bunch of strings. Hence, the dates will not be arranged in a timely order.

republished_df['action_date'] = pd.to_datetime(republished_df['action_date']) login_df['action_date'] = pd.to_datetime(login_df['action_date']) subscription_info_df['subscription_starts_at'] = pd.to_datetime(subscription_info_df['subscription_starts_at']) subscription_info_df['subscription_ends_at'] = pd.to_datetime(subscription_info_df['subscription_ends_at'])

Let’s get sample data for an account for this tutorial.

sample_subscription = subscription_info_df[subscription_info_df['AccountCode'] == 'a'] sample_republished = republished_df[republished_df['AccountCode'] == 'a'] sample_login = login_df[login_df['AccountCode'] == 'a']



# This is a constant for visualization purpose

sample_subscription['vizline'] = 0.5

sample_republished['vizline'] = 0.5

sample_login['vizline'] = 0.5

TIP #1: Is this account a same-day flapper? Let’s mix some colors

This tip is handy when we need to visualize different events that only happen once, but they may happen on the same day.

Like any subscription-based company, Unbounce expects flappers — subscribers who subscribe, churn, then come back at some point in time. There are cases when churn and re-subscription happen on the same date. To distinguish same-day flappers, we can use this color mixing trick.

Note: we assume here that each subscription is mutually exclusive to another.

If we visualize subscription start date with a different color than subscription end date and use some opacity level, we will have a different color for same-day flappers.

For example, here I choose blue for subscription start date and red for subscription end date , and change opacity level through alpha = 0.5 ( alpha ranges from 0 to 1). This results in magenta for same-day flappers.

You can learn more about the basics of color mixing through this article.

Here is a list of color codes in Matplotlib.