def plot_dau_mau_ratio()

in jobs/desktop-mobile-mau-2020/desktop_mau/desktop_mau_dau.py [0:0]


def plot_dau_mau_ratio(desktop_data):
    ind_dat = pd.merge(
        desktop_data, desktop_data[desktop_data["country"] == "Global"], on=["date"]
    ).sort_values(by=["country_x", "date"])
    ind_dat["DAU_global_7dMA"] = ind_dat["DAU_y"].rolling(window=7).mean()
    ind_dat["DAU_7dMA"] = ind_dat["DAU_x"].rolling(window=7).mean()
    ind_dat["pcnt_MAU"] = ind_dat["MAU_x"] / ind_dat["MAU_y"]
    ind_dat["pcnt_DAU"] = ind_dat["DAU_7dMA"] / ind_dat["DAU_global_7dMA"]
    ind_dat = ind_dat[ind_dat["date"] >= pd.to_datetime("20190101")]

    plt.style.use("seaborn-white")
    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(20, 5), sharex="col")

    country_list = ["US", "CA", "DE", "FR", "GB"]
    metric_list = ["MAU", "DAU"]
    for i, metric in enumerate(metric_list):
        for country in country_list:
            axs[i].plot(
                ind_dat[ind_dat["country_x"] == country]["date"],
                ind_dat[ind_dat["country_x"] == country]["pcnt_" + metric],
                label=str(metric),
                linestyle="solid",
            )
        axs[i].set(ylim=(0, 0.20))
        axs[i].xaxis.set_major_formatter(mdates.DateFormatter("%Y-%b"))
        axs[i].yaxis.set_major_formatter(
            matplotlib.ticker.FuncFormatter(
                lambda x, pos: "{:,.0f}".format(x * 100) + "%"
            )
        )
        axs[i].legend(
            country_list,
            bbox_to_anchor=(0.9, 0.8, 0.2, 0.2),
            loc="upper left",
            ncol=1,
            mode="expand",
            fontsize=10,
        )
        axs[i].grid(which="major", linestyle="-", linewidth="0.5", color="lightgray")

    axs[0].set_title(
        "Contribution of MAU/DAU per Tier1 country as of "
        + ind_dat["date"].max().strftime("%Y-%m-%d"),
        loc="left",
        fontdict={"fontsize": 18, "color": "black"},
    )
    filename_1 = "desktop_mau_dau_ratio.jpeg"
    plt.savefig(IMG_DIR / filename_1)
    plt.close(fig)

    ind_dat = pd.merge(
        desktop_data[desktop_data["country"].isin(["US", "CA", "DE", "FR", "GB"])],
        desktop_data[desktop_data["country"] == "Global"],
        on=["date"],
    )
    ind_dat["pcnt_MAU"] = ind_dat["MAU_x"] / ind_dat["MAU_y"]
    ind_dat = ind_dat[ind_dat["date"] >= pd.to_datetime("20190101")]

    plt.style.use("seaborn-white")
    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(20, 5), sharex="col")

    metric = "MAU"
    country_list = ["US", "CA", "DE", "FR", "GB"]
    for country in country_list:
        axs[0].plot(
            ind_dat[ind_dat["country_x"] == country]["date"],
            ind_dat[ind_dat["country_x"] == country]["pcnt_" + metric],
            label=str(metric),
            linestyle="solid",
        )
    axs[0].set(ylim=(0, 0.20))
    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,
    )
    axs[0].grid(which="major", linestyle="-", linewidth="0.5", color="lightgray")

    axs[0].set_title(
        "Contribution of MAU/DAU per Tier1 country as of "
        + ind_dat["date"].max().strftime("%Y-%m-%d"),
        loc="left",
        fontdict={"fontsize": 18, "color": "black"},
    )

    # axs[1].plot.area([ind_dat["country_x"]\
    # .isin(country_list)]["date", "country_x", "pcnt"], subplots=True)
    dat2 = ind_dat[ind_dat["country_x"].isin(country_list)][
        ["date", "country_x", "pcnt_MAU"]
    ].pivot(index="date", columns="country_x", values="pcnt_MAU")
    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")
    filename_2 = "desktop_mau_dau_ratio_2.jpeg"
    plt.savefig(IMG_DIR / filename_2)
    plt.close(fig)

    return filename_1, filename_2