Page MenuHomec4science

plot_government.py
No OneTemporary

File Metadata

Created
Fri, Jun 28, 12:50

plot_government.py

import pandas as pd
import matplotlib.pyplot as plt
def plot(path):
complete_data = pd.read_csv(path,
encoding="utf-8-sig")
parties = complete_data.loc[complete_data["affiliation_category"] == "parties"]
metadata = pd.read_csv("../data/meetings_metadata.csv")
result_df = pd.DataFrame(columns={"label", "government", "diplomacy",
"security", "press", "no description",
"no keyword found"})
for label in metadata["label"]:
this_meeting = parties.loc[parties["meeting"] == label]
by_party = this_meeting.groupby("affiliation")
gov_acc = 0
dipl_acc = 0
sec_acc = 0
press_acc = 0
nodescr_acc = 0
nokeyword_acc = 0
for aff, people in by_party:
counter = len(people)
government_repr = people.loc[people["role"] == "government"]
gov_acc += len(government_repr) / counter
dipl_acc += len(people.loc[people["role"] == "diplomacy"]) / counter
sec_acc += len(people.loc[people["role"] == "security"]) / counter
press_acc += len(people.loc[people["role"] == "press"]) / counter
nodescr_acc += len(people.loc[people["role"] == "no description"]) / counter
nokeyword_acc += len(people.loc[people["role"] == "no keyword found"]) / counter
n_parties = len(by_party)
result_df = result_df.append({
"label": label,
"government": gov_acc / n_parties,
"diplomacy": dipl_acc / n_parties,
"security": sec_acc / n_parties,
"press": press_acc / n_parties,
"no description": nodescr_acc / n_parties,
"no keyword found": nokeyword_acc / n_parties
}, ignore_index=True)
# First, plot for COPs
ordered_categories = ["government", "diplomacy", "security", "press", "no keyword found", "no description"]
colors = {"no description": "C5", "government": "C1", "diplomacy": "C2",
"security": "C6", "press": "C4", "no keyword found": "C0"}
plot_data = result_df.loc[result_df.label.apply(lambda l: l.startswith("cop"))]
plot_data = plot_data.set_index("label")
plot_data = pd.DataFrame(plot_data)
plot_data.columns = pd.CategoricalIndex(plot_data.columns.values,
ordered=True,
categories=ordered_categories)
# Sort the columns (axis=1) by the new categorical ordering
plot_data = plot_data.sort_index(axis=1)
plot_data.plot(kind="area", xlabel="Meeting", ylabel="Avg rate of roles per party",
title="Roles in parties (COP)", stacked=True, color=colors, ylim=[0,1])
# Second, plot for SBs
plot_data2 = result_df.loc[result_df.label.apply(lambda l: l.startswith("sb"))]
plot_data2 = plot_data2.set_index("label")
#plot_data2 = plot_data2.unstack(level=0)
plot_data2 = pd.DataFrame(plot_data2)
plot_data2.columns = pd.CategoricalIndex(plot_data2.columns.values,
ordered=True,
categories=ordered_categories)
plot_data2 = plot_data2.sort_index(axis=1)
plot_data2.plot(kind="area", xlabel="Meeting", ylabel="Avg rate of roles per party",
title="Roles in parties (SB)", stacked=True, color=colors, ylim=[0,1])
plt.show()

Event Timeline