def plot(args): # Set SIGCONF settings. sigconf_settings() # Load data. with open(args.data, 'rb') as f: (mun_votes, mun_info) = pickle.load(f) print(f'Loaded {args.data}') # Filter data. lang_avail = (~mun_info.language.isna()) cant_avail = (~mun_info.canton.isna()) mun_votes = mun_votes[lang_avail & cant_avail] mun_info = mun_info[lang_avail & cant_avail] mun_info = mun_info.drop([3805, 3810]) mun_votes = mun_votes.drop([3805, 3810]) # Compute SVD embedding. embedding = svd(mun_votes.values) # Define colors. colors = { 'fr': 'C0', 'de': 'C1', 'ro': 'C2', 'it': 'C3', 'unknown': 'black' } # Define labels. labels = { 'fr': 'French', 'de': 'German', 'ro': 'Romansh', 'it': 'Italian' } # Plot. fig, ax = plt.subplots(ncols=1, figsize=(3.6, 1.8)) plot_svd_with_lang( embedding=embedding, languages=list(mun_info.language), colors=colors, labels=labels, fig=fig, ax=ax ) fig.tight_layout() plt.savefig(args.fig) print(f'Figure saved to {args.fig}')
def main(args): # Set SIGCONF settings. sigconf_settings() # Load data. ds = GermanParliamentRegion(args.data) data = ds.M.reshape(ds.shape[0], -1) print(f'Loaded {args.data}') parties = ['SPD', 'CDU/CSU', 'Greens', 'FDP', 'Left'] colors = ['C3', 'C0', 'C2', 'C1', 'C6'] colors = [f'C{i}' for i in range(len(parties))] colorixs = np.argsort(ds.M.mean(axis=1), axis=1) # Get SVD embedding. if args.embedding is not None: with open(args.embedding, 'rb') as f: (embedding, colorixs) = pickle.load(f) print(f'Loaded embedding from {args.embedding}') else: embedding = svd(data) path = f'{args.data}/de-embedding.pkl' with open(path, 'wb') as f: pickle.dump((embedding, colorixs), f) print(f'Saved embedding in {path}') # Plot. fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(3.6, 2)) plot_nth_scatter(embedding=embedding, n=1, ax=ax1, colorixs=colorixs, colors=colors, parties=parties, title='Coloring by First Party', print_names=True) plot_nth_scatter(embedding=embedding, n=3, ax=ax2, colorixs=colorixs, colors=colors, parties=parties, title='Coloring by Third Party', print_names=True) fig.tight_layout() plt.savefig(args.fig) print(f'Figure saved to {args.fig}')
def main(args): # Set SIGCONF settings. sigconf_settings() # Load predictions. data = load(args.data) # Define labels. labels = [ 'Averaging', r'\textsc{SubSVD-Bernoulli}', ] # Define colors. colors = [ 'black', 'C3', ] # Define line styles. linestyles = [ '-', # '--', '-' ] # Set plot positions. positions = ['top', 'bottom'] # Set titles. titles = ['Affordable Houses', 'Ban on Homophobia'] fig, axes = plt.subplots(nrows=2, figsize=(3.6, 2.8)) for vote, preds in data.items(): if vote not in DATE2VOTES['2020-02-09']: continue i = VOTE2IDX[vote] plot(preds, axes[i], labels, colors, linestyles, titles[i], positions[i]) fig.tight_layout() plt.savefig(args.fig) print(f'Figure saved to {args.fig}')
def plot(args): # Set SIGCONF settings. sigconf_settings() # Setup figure layout. gridspec = dict(wspace=0.1, bottom=0.2, right=0.95) fig, axes = plt.subplots( nrows=2, ncols=2, figsize=(3.6, 2.66), sharex=False, sharey='row', gridspec_kw=gridspec ) # Plot district and state results. plot_state(args, fig, axes[0, 0], axes[1, 0]) plot_district(args, fig, axes[0, 1], axes[1, 1]) # Plot and save figure. plt.savefig(args.fig) print(f'Figure saved to {args.fig}')
def plot(args): # Set SIGCONF settings. sigconf_settings() # Define type of plot. mae = 'mae-outcome' acc = 'nat_correct' # Set keys to models. models = [ 'Weighted Averaging', 'Matrix Factorization (dim=25,lam_V=0.03,lam_U=31.0)', # 'Weighted SubSVD (dim=25,l2=0.1)', 'Weighted Logistic SubSVD (dim=25,l2=0.1)', ] # Define labels. labels = [ 'Averaging', 'MF', # r'\textsc{SubSVD-Gaussian}', r'\textsc{SubSVD-Bernoulli}', ] # Define line styles. lines = [ '-', ':', '--', ] # Define colors. colors = ['black', 'black', 'C3'] # Define plot title. title = 'Swiss Referenda' # Load data. results = load(args.data) # Plot. fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(3.5, 2.66)) _plot( # MAE. results=results, metric=mae, title=title, models=models, labels=labels, lines=lines, colors=colors, regions='municipalities', position='top', ax=ax1, fig=fig, x_logscale=True) _plot( # Accuracy. results=results, metric=acc, title=title, models=models, labels=labels, lines=lines, colors=colors, position='bottom', regions='municipalities', ax=ax2, fig=fig, x_logscale=True) fig.tight_layout() plt.savefig(args.fig) print(f'Figure saved to {args.fig}')