Пример #1
0
def plot_cluster(clusters, df):
    fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(16, 5))
    for i, cluster in enumerate(clusters):
        ax = axes[i // 5][i % 5]
        years, rates = [], []
        for t in [0, 0.2, 0.4]:
            ax.axhline(t, linestyle="--", lw=1, color="#999999")
        for year, tdf in df.groupby("year"):
            years.append(year)
            pos = len(tdf[tdf[cluster]])
            total = len(tdf)
            low, high = pc(pos, total)
            ax.plot([year, year], [low, high],
                    color=color_dict[cluster],
                    lw=lw,
                    alpha=alpha)
            rates.append(pos / total)
        ax.plot(years,
                rates,
                color=color_dict[cluster],
                lw=lw,
                alpha=alpha,
                marker="o")
        ax.set_xlim([2009.5, 2020.5])
        if i // 5 == 0:
            ax.set_xticks([2010, 2015, 2020])
            ax.set_xticklabels(["  till ’10", "’15 ", "’20    "])
        else:
            ax.set_xticks([])
            ax.set_xticklabels([])
        ax.xaxis.tick_top()
        ax.set_xlabel(bebold(cluster))
        ax.set_ylim([-0.02, 0.58])
        if i % 5 == 0:
            ax.set_yticks([0, 0.2, 0.4])
            ax.set_yticklabels(["0", ".2", ".4"])
        else:
            ax.set_yticks([])
            ax.set_yticklabels([])
        for edge in ["right", "left", "top", "bottom"]:
            ax.spines[edge].set_visible(False)
        plt.savefig(os.path.join(figs_path, "analyze_clusters.pdf"),
                    bbox_inches="tight",
                    pad_inches=0)
Пример #2
0
                                "xticks": [0, 0.05, 0.1, 0.15, 0.2, 0.25],
                                "xticklabels": ["0%", "5%", "10%", "15%", "20%", "25%"]},
                     "disbelief": {"c": "indianred",
                                   "xlim": [0, 0.25],
                                   "xticks": [0, 0.05, 0.1, 0.15, 0.2],
                                   "xticklabels": ["0%", "5%", "10%", "15%", "20%"]}}
        fig_path = os.path.join(sys_path, "results", label + "_platform.pdf")
        fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(3, 1.2))
        for i, (site, gdf) in enumerate(df[df["before_fc"] == True].groupby("site")):
            positive_count = gdf[gdf[label] == True]["count"].values[0]
            negative_count = gdf[gdf[label] == False]["count"].values[0]
            total_count = positive_count + negative_count
            postitive_rate = positive_count / total_count
            print(site, postitive_rate)
            ax.barh(i, postitive_rate, color=plot_args[label]["c"], alpha=0.8)
            ci_low, ci_upp = pc(positive_count, total_count, alpha=0.05/3)
            ax.plot([ci_low, ci_upp], [i, i], color="k", alpha=0.8)
            ax.text(ci_upp, i, " " + str(int(round(postitive_rate * 100))) + "%",
                    ha="left", va="center", color="gray", alpha=0.8, fontproperties=font)
            ax.set_xlim(plot_args[label]["xlim"])
            ax.set_xticks(plot_args[label]["xticks"])
            ax.set_xticklabels(plot_args[label]["xticklabels"])
        ax.set_xlabel(label.capitalize() + " %", fontproperties=font2)
        ax.set_ylim([-0.6, 2.6])
        ax.set_yticks([0, 1, 2])
        ax.set_yticklabels(["Facebook", "Twitter", "YouTube"])
        ax.set_ylabel("Platform", fontproperties=font2)
        ax.spines["right"].set_visible(False)
        ax.spines["top"].set_visible(False)
        plt.savefig(fig_path, bbox_inches = "tight", pad_inches = 0)
Пример #3
0
def plot_event(event_pairs, df):
    fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(5, 8))
    for i, (event_pair, events) in enumerate(event_pairs.items()):
        e0, e1 = events[0], events[1]
        tdf0, tdf1 = df[df[events[0]]], df[df[events[1]]]
        if "election" in event_pair:
            tdf0 = tdf0[tdf0["year"] == 2016]
            tdf1 = tdf1[tdf1["year"] == 2020]
        else:
            tdf0 = tdf0[tdf0["year"] < 2013]
            tdf1 = tdf1[tdf1["year"] > 2018]
        total0 = len(tdf0)
        total1 = len(tdf1)
        for j, cluster in enumerate(clusters):
            pos0 = len(tdf0[tdf0[cluster]])
            low0, high0 = pc(pos0, total0)
            pos1 = len(tdf1[tdf1[cluster]])
            low1, high1 = pc(pos1, total1)

            if low0 > high1 or low1 > high0:
                ax[i].axhline(j, lw=35, color="#D8D8D8")
            d = 0.15
            ax[i].plot([low0, high0], [j - d, j - d],
                       color=color_dict[cluster],
                       lw=lw,
                       alpha=alpha)
            ax[i].plot([pos0 / total0], [j - d],
                       color=color_dict[cluster],
                       marker=markers[i][0],
                       markersize=int(markers[i][2]))
            ax[i].plot([low1, high1], [j + d, j + d],
                       color=color_dict[cluster],
                       lw=lw,
                       alpha=alpha)
            ax[i].plot([pos1 / total1], [j + d],
                       color=color_dict[cluster],
                       marker=markers[i][1],
                       markersize=int(markers[i][2]))
        ax[i].set_ylim([9.5, -0.5])
        ax[i].plot(-1,
                   -1,
                   color="k",
                   label=e0,
                   marker=markers[i][0],
                   markersize=int(markers[i][2]))
        ax[i].plot(-1,
                   -1,
                   color="k",
                   label=e1,
                   marker=markers[i][1],
                   markersize=int(markers[i][2]))
        leg = ax[i].legend(bbox_to_anchor=(1, 1.05, 0, 0),
                           loc="lower right",
                           borderaxespad=0.,
                           fontsize=16)
        leg.get_frame().set_alpha(0)
    ax[0].set_xlim([0, 0.4])
    ax[0].set_xticks([0, 0.2, 0.4])
    ax[0].set_xticklabels([" 0", ".2 ", ".4  "])
    ax[1].set_xlim([0, 0.65])
    ax[1].set_xticks([0, 0.3, 0.6])
    ax[1].set_xticklabels([" 0", ".3 ", ".6  "])
    ax[0].set_yticks([9 - i for i in range(10)])
    ax[0].set_yticklabels([bebold(clusters[9 - i]) for i in range(10)])
    ax[1].set_yticks([])
    ax[1].set_yticklabels([])
    for edge in ["right", "left", "top", "bottom"]:
        ax[0].spines[edge].set_visible(False)
        ax[1].spines[edge].set_visible(False)
    plt.savefig(os.path.join(figs_path, "analyze_events.pdf"),
                bbox_inches="tight",
                pad_inches=0)
    control_set = [
        "moderated", "meta_like", "meta_dislike", "meta_view",
        "linguist_swear", "linguist_laugh", "linguist_emoji", "linguist_fake",
        "linguist_administration", "linguist_american", "linguist_nation",
        "linguist_personal", "misinfo_factcheck", "misinfo_veracity",
        "bias_degree", "bias_party"
    ]

    # plot correlation
    df = pd.read_csv(comments_path)
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(2.5, 0.6))
    w = 0.4
    for i, cause in enumerate(cause_set):
        control = df[df[cause] == 0]["moderated"].mean()
        treated = df[df[cause] == 1]["moderated"].mean()
        control_pc = pc(df[df[cause] == 0]["moderated"].sum(),
                        df[df[cause] == 0]["moderated"].count())
        control_err = control - control_pc[0]
        treated_pc = pc(df[df[cause] == 1]["moderated"].sum(),
                        df[df[cause] == 1]["moderated"].count())
        treated_err = treated - treated_pc[0]
        ax.barh(4 - i - w,
                width=control,
                height=w,
                color="cornflowerblue",
                alpha=0.9)
        ax.plot([control_pc[0], control_pc[1]], [4 - i - w, 4 - i - w],
                color="k",
                alpha=0.5)
        ax.text(control_pc[1] + 0.001,
                4 - i - w,
                "{0:.2f}".format(round(control * 100, 2)) + "%",