def plot_dau_user_state_individual_country()

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


def plot_dau_user_state_individual_country(full_dat, country):
    """

    Save a plot with subplots showing the contribution of each user state to DAU
    Data is for this specific country only

    """
    full_dat["dau"] = (
        full_dat["dau_regular"]
        + full_dat["dau_new_or_resurrected"]
        + full_dat["dau_other"]
    )
    dat = (
        full_dat[["date", "dau", "dau_regular", "dau_new_or_resurrected", "dau_other"]]
        .melt(id_vars=["date", "dau"])
        .rename(columns={"variable": "user_state"})
        .sort_values("date")
    )

    dat["user_state"] = dat["user_state"].str.replace("dau_", "")
    dat["pcnt"] = dat["value"] / dat["dau"]

    plt.style.use("seaborn-white")
    fig, axs = plt.subplots(nrows=1, ncols=4, figsize=(18, 4), sharex="col")
    dat = dat[dat["date"] >= dat["date"].max() + timedelta(-182)]
    cat_list = ["new_or_resurrected", "regular", "other"]

    # individual
    for cat in cat_list:
        axs[0].plot(
            dat[dat["user_state"] == cat]["date"],
            dat[dat["user_state"] == cat]["pcnt"],
            label=str(cat),
            linestyle="solid",
        )
    axs[0].set(ylim=(0, 1))
    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(
        cat_list,
        bbox_to_anchor=(0.5, 0.8, 0.2, 0.2),
        loc="upper left",
        ncol=1,
        mode="expand",
        fontsize=10,
    )

    # stacked
    dat2 = dat[dat["user_state"].isin(cat_list)][["date", "user_state", "pcnt"]].pivot(
        index="date", columns="user_state", values="pcnt"
    )
    dat2 = pd.DataFrame(data=dat2)
    y = np.vstack([dat2[x] for x in cat_list])
    axs[1].stackplot(dat2.index, y, labels=cat_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) + "%")
    )

    # New only, as it"s small
    cat = "new_or_resurrected"
    axs[2].plot(
        dat[dat["user_state"] == cat]["date"],
        dat[dat["user_state"] == cat]["pcnt"],
        label=str(cat),
        linestyle="solid",
    )
    axs[2].xaxis.set_major_formatter(mdates.DateFormatter("%y-%b"))
    axs[2].yaxis.set_major_formatter(
        matplotlib.ticker.FuncFormatter(lambda x, pos: "{:,.0f}".format(x * 100) + "%")
    )
    axs[2].legend(
        bbox_to_anchor=(0.5, 0.8, 0.2, 0.2),
        loc="upper left",
        ncol=1,
        mode="expand",
        fontsize=10,
    )

    # Regular only
    cat = "regular"
    axs[3].plot(
        dat[dat["user_state"] == cat]["date"],
        dat[dat["user_state"] == cat]["pcnt"],
        label=str(cat),
        linestyle="solid",
        color="orange",
    )
    axs[3].xaxis.set_major_formatter(mdates.DateFormatter("%y-%b"))
    axs[3].yaxis.set_major_formatter(
        matplotlib.ticker.FuncFormatter(lambda x, pos: "{:,.0f}".format(x * 100) + "%")
    )
    axs[3].legend(
        bbox_to_anchor=(0.6, 0.1, 0.2, 0.2),
        loc="upper left",
        ncol=1,
        mode="expand",
        fontsize=10,
    )

    axs[0].set_title(
        "DAU per User State (individual/stacked) from {}".format(country),
        loc="left",
        fontdict={"fontsize": 18, "color": "black"},
    )

    filename = f"desktop_{country}_user_state_dau_contribution.jpeg"
    plt.savefig(IMG_DIR / filename)
    plt.close(fig)
    return filename