in jobs/desktop-mobile-mau-2020/desktop_mau/desktop_mau_dau.py [0:0]
def plot_group_contribution(full_data, country_list, cat, metric):
full_data = full_data[["date", "country", metric]]
if metric == "MAU":
dat = pd.merge(
full_data[full_data["country"].isin(country_list)],
full_data[full_data["country"] == "Global"],
on=["date"],
).sort_values(by=["country_x", "date"])
dat["pcnt"] = dat["MAU_x"] / dat["MAU_y"]
if metric == "DAU":
dat = pd.merge(
full_data[full_data["country"].isin(country_list)],
full_data[full_data["country"] == "Global"],
on=["date"],
).sort_values(by=["country_x", "date"])
dat["DAU_global_7dMA"] = dat["DAU_y"].rolling(window=7).mean()
dat["DAU_7dMA"] = dat["DAU_x"].rolling(window=7).mean()
dat["pcnt"] = dat["DAU_7dMA"] / dat["DAU_global_7dMA"]
plt.style.use("seaborn-white")
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(20, 5), sharex="col")
dat = dat[dat["date"] >= pd.to_datetime("20190101")]
# individual
for country in country_list:
axs[0].plot(
dat[dat["country_x"] == country]["date"],
dat[dat["country_x"] == country]["pcnt"],
label=str(country),
linestyle="solid",
)
axs[0].set(ylim=(0, 0.2))
axs[0].xaxis.set_major_formatter(mdates.DateFormatter("%Y-%b"))
axs[0].yaxis.set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, pos: "{:,.0f}".format(x * 100) + "%")
)
axs[0].legend(
country_list,
bbox_to_anchor=(0.9, 0.8, 0.2, 0.2),
loc="upper left",
ncol=1,
mode="expand",
fontsize=10,
)
# stacked
dat2 = dat[dat["country_x"].isin(country_list)][
["date", "country_x", "pcnt"]
].pivot(index="date", columns="country_x", values="pcnt")
dat2 = pd.DataFrame(data=dat2)
y = np.vstack([dat2[x] for x in country_list])
axs[1].stackplot(dat2.index, y, labels=country_list, alpha=0.5)
# axs[1].set(ylim=(0, 0.4))
axs[1].xaxis.set_major_formatter(mdates.DateFormatter("%Y-%b"))
axs[1].yaxis.set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, pos: "{:,.0f}".format(x * 100) + "%")
)
axs[1].legend(
country_list,
bbox_to_anchor=(0.9, 0.8, 0.2, 0.2),
loc="upper left",
ncol=1,
mode="expand",
fontsize=10,
)
axs[1].grid(which="major", linestyle="-", linewidth="0.5", color="lightgray")
axs[0].set_title(
"Contribution of {} from Tier1 as of {} (non-stacked/stacked)".format(
metric, dat["date"].max().strftime("%Y-%m-%d")
),
loc="left",
fontdict={"fontsize": 18, "color": "black"},
)
filename = f"desktop_{metric}_{cat}_contribution.jpeg"
plt.savefig(IMG_DIR / filename)
plt.close(fig)
return filename