Created
December 22, 2022 00:14
-
-
Save robd003/c317e2abb0676c9e1f2c0633f43ebb4c to your computer and use it in GitHub Desktop.
Django plot user growth
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
from django.contrib.auth.models import User | |
from datetime import datetime, timedelta | |
import pytz | |
import time | |
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
pd.set_option('display.max_rows', 500) | |
pd.set_option('display.max_columns', 500) | |
pd.set_option('display.width', 1000) | |
# Set a start date | |
start_date = datetime(2020, 7, 13, tzinfo=pytz.UTC) | |
today = pytz.utc.localize(datetime.today()) | |
# Setup for weekly metrics | |
week_start = start_date | |
weeks = [] | |
while week_start <= today: | |
weeks.append(week_start) | |
week_start += timedelta(days=7) | |
week_strings = [week.strftime("%d/%m") for week in weeks] | |
# Count number of cumulative sign ups | |
tot_users = [] # total users | |
pc_growth = [] # percentage growth week on week | |
for (index, week) in enumerate(weeks): | |
weekly_signups = User.objects.filter(is_active=True).filter(date_joined__gte=start_date).filter(date_joined__lt=weeks[index]) | |
tot_users.append(len(weekly_signups)) | |
if index > 1: | |
pc_growth.append(np.around((100* (tot_users[index] - tot_users[index-1]))/(tot_users[index-1]), 2)) | |
else: | |
pc_growth.append(0) | |
users_per_week_df = pd.DataFrame({"tot_users":tot_users, "pc_growth":pc_growth}, index=week_strings) | |
print(np.mean(np.around(pc_growth, 2))) | |
print(users_per_week_df) | |
fig, axs = plt.subplots(figsize=(10,6)) | |
axs.plot(users_per_week_df["tot_users"]) | |
axs.set_xticks(axs.get_xticks()[::4]) | |
axs.set_xlabel("Date") | |
axs.set_ylabel("Total Users") | |
axs.set_title("Total User Signups",{'fontsize': 18, 'fontweight': 600}) | |
axs.grid() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment