This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig, ax = plt.subplots(figsize=(8, 20)) | |
ax = sns.heatmap(data, cmap="Blues", ax=ax, cbar=False, xticklabels=names) | |
ax.xaxis.tick_top() | |
ax.set_ylabel("Ballot Count for Combination") | |
plt.xticks(rotation=90) | |
plt.yticks(ticks=yticks, labels=ytick_labels, fontsize=5) | |
for border in borders: | |
ax.axhline(border, color="white", lw=0.2) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yticks = [] | |
ytick_labels = [] | |
borders = [0] | |
data = [] | |
prev = 0 | |
for i in range(df_comb.shape[0]): | |
values = df_comb.iloc[i].tolist() | |
*values, count = values | |
yticks.append(prev + count//2) | |
ytick_labels.append(count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_comb = df_cvr.groupby(names.tolist()) | |
df_comb = df_comb.size().reset_index(name="Count") | |
df_comb = df_comb.sort_values("Count", ascending=False) | |
df_comb = df_comb.reset_index(drop=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ax = sns.histplot(df_cvr.sum(axis=1), discrete=True) | |
ax.set_xlabel("Marks Per Ballot") | |
_ = plt.xticks([1, 2, 3]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_hist = df_contest.index.get_level_values("candidate").remove_unused_categories() | |
data_hist = data_hist.reorder_categories(new_categories=names) | |
g = sns.histplot(data_hist) | |
plt.xticks(rotation=90) | |
g.set_xlabel("") | |
_ = g.set_ylabel("Votes") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_contest = df_raw.loc[idx[:, "City of Los Altos, Council Member (Vote For=3)"], :] | |
df_contest.index = df_contest.index.droplevel("contest") | |
df_cvr = df_contest.astype(int).unstack(fill_value=0) | |
df_cvr.columns = df_cvr.columns.droplevel(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cluster_order(d): | |
pdist = hierarchy.distance.pdist(d.values) | |
linkage = hierarchy.linkage(pdist, method="complete") | |
idx = hierarchy.fcluster(linkage, 0.5 * pdist.max(), "distance") | |
idx = np.argsort(idx) | |
return d.iloc[idx, idx] | |
def draw(df, symmetric=True): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import matplotlib | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pandas as pd | |
import seaborn as sns | |
from scipy.cluster import hierarchy | |
from matplotlib.colors import ListedColormap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig, ax = plt.subplots(figsize=(8, 20)) | |
ax = sns.heatmap(data, cmap="Blues", ax=ax, cbar=False, xticklabels=names) | |
ax.xaxis.tick_top() | |
ax.set_ylabel("Ballot Count for Combination") | |
plt.xticks(rotation=90) | |
plt.yticks(ticks=yticks, labels=ytick_labels, fontsize=5) | |
for border in borders: | |
ax.axhline(border, color="white", lw=0.2) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yticks = [] | |
ytick_labels = [] | |
borders = [0] | |
data = [] | |
prev = 0 | |
for i in range(df_comb.shape[0]): | |
values = df_comb.iloc[i].tolist() | |
*values, count = values | |
yticks.append(prev + count//2) | |
ytick_labels.append(count) |
NewerOlder