in jobs/desktop-mobile-mau-2020/desktop_mau/desktop_mau_dau.py [0:0]
def plot_dau_mau_contribution_individual_country(full_dat, country):
"""
Save a plot with subplots showing the contribution of DAU/MAU w.r.t. global
"""
new_dat = (
pd.merge(
full_dat[full_dat["country"] == country],
full_dat[full_dat["country"] == "Global"],
on=["date"],
)
.rename(
columns={
"MAU_x": "MAU",
"DAU_x": "DAU",
"MAU_y": "MAU_global",
"DAU_y": "DAU_global",
}
)
.sort_values(by="date")
)
new_dat["DAU_global_7dMA"] = new_dat["DAU_global"].rolling(window=7).mean()
new_dat["DAU_7dMA"] = new_dat["DAU"].rolling(window=7).mean()
new_dat["pcnt_MAU"] = new_dat["MAU"] / new_dat["MAU_global"]
new_dat["pcnt_DAU"] = new_dat["DAU_7dMA"] / new_dat["DAU_global_7dMA"]
plt.style.use("seaborn-white")
fig = plt.figure(figsize=(12, 6))
ax = plt.axes(label=f"desktop_{country}_mau_dau_ratio")
ax.set(xlim=(pd.to_datetime("20190101"), new_dat["date"].max()))
ax.plot(
new_dat["date"],
new_dat["pcnt_MAU"],
color="#0000ff",
linestyle="solid",
label="MAU",
)
ax.plot(
new_dat["date"],
new_dat["pcnt_DAU"],
color="gray",
linestyle="dashdot",
label="DAU_7dMA",
)
plt.title(
label="Contribution of MAU/DAU from {} as of {}".format(
country, new_dat["date"].max().strftime("%Y-%m-%d")
),
loc="left",
fontdict={"fontsize": 20, "color": "black"},
)
plt.xlabel("Date", fontsize=16)
ax.xaxis.set_major_locator(matplotlib.dates.YearLocator())
ax.xaxis.set_minor_locator(
matplotlib.dates.MonthLocator((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))
)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("\n%Y"))
ax.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter("%b %d"))
ax.yaxis.set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, pos: "{:,.0f}".format(x * 100) + "%")
)
plt.setp(ax.get_xticklabels(), rotation=0, ha="center", fontsize=14)
plt.setp(ax.get_yticklabels(), rotation=0, ha="right", fontsize=12)
ax.legend(
bbox_to_anchor=(0.9, 0.9),
loc=3,
ncol=2,
mode="expand",
borderaxespad=0.0,
fontsize=10,
)
ax.grid(which="major", linestyle="-", linewidth="0.5", color="lightgray")
filename = f"desktop_{country}_mau_dau_ratio.jpeg"
plt.savefig(IMG_DIR / filename)
plt.close(fig)
return filename