Skip to content

Commit

Permalink
plotting susceptibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
traducha committed May 27, 2022
1 parent 0aa6107 commit b6ce771
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def f(n, g):
# short suffix in case of using a configuration file
if self.config_file is not None:
self.suffix = (f"_{self.config_file.split('/')[-1].replace('.json', '')}_p_{self.propagation}"
f"_media_{self.mass_media}_zn_{self.n_zealots}")
f"_media_{self.mass_media}_zn_{self.n_zealots}_mc_{self.mc_steps}")

# at the end remove dots from the suffix so latex doesn't have issues with the filenames
self.suffix = self.suffix.replace('.', '')
Expand Down
38 changes: 38 additions & 0 deletions plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,44 @@ def plot_mean_std_two_systems(x, y1, std1, y1_name, y2, std2, y2_name, quantity,
plt.close('all')


def plot_mean_std_all(voting_systems, x, results, quantity, suffix, xlab, ylab='election result of a', ylim=(),
save_file=True):
"""
Plots mean +/- std of all electoral systems from results vs quantity (eg number of zealots)
:param voting_systems: iterable with names of electoral systems
:param x: array with considered quantity (zealots / media influence)
:param results: dict with results with keys 'mean_set' and 'std_set' (e.g. prepared in zealot_susceptibility.py)
:param quantity: we calculate susceptibility over that quantity
:param suffix: suffix with params values
:param xlab: x-axis label
:param ylab: y-axis label
:param ylim: y-axis limits as [ymin, ymax]
:param save_file: bool if save plot to file
"""
plt.figure(figsize=(4, 3))

for i, system in enumerate(voting_systems):
plt.plot(x, results[system]['mean_set'], color=COLORS[i % len(COLORS)], linestyle='-', label=system)
plt.fill_between(x, results[system]['mean_set'] - results[system]['std_set'],
results[system]['mean_set'] + results[system]['std_set'],
color=COLORS[i % len(COLORS)], linewidth=0, alpha=0.5)

if ylim:
plt.ylim(ylim)

plt.title(f'{quantity} susceptibility')
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.legend()
plt.tight_layout()
if save_file:
s = suffix.format(valuetoinsert='')
plt.savefig(f'plots/{quantity}_sus_all{s}.png')
else:
plt.show()
plt.close('all')


def plot_std(x, std, quantity, election_system, suffix, xlab,
ylab='election result of 1', ylim=(), save_file=True):
"""
Expand Down
13 changes: 5 additions & 8 deletions scripts/media_susceptibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from configuration.parser import get_arguments
from tools import convert_to_distributions, split_suffix
from plotting import plot_mean_std, plot_heatmap, plot_std, plot_mean_per, plot_mean_diff, plot_mean_std_two_systems
from plotting import plot_mean_std, plot_heatmap, plot_std, plot_mean_per, plot_mean_diff, plot_mean_std_all

# parameters of simulations not present in the config module
media_influence = np.linspace(0, 1, 21) # range of considered media influence
Expand Down Expand Up @@ -80,13 +80,10 @@ def plot_media_susceptibility(config):
suffix=suffix, xlab='media influence', ylab=f'susceptibility derivative', ylim=(),
save_file=True)

plot_mean_std_two_systems(
x=media_influence, y1=results['countrywide_system']['mean_set'], std1=results['countrywide_system']['std_set'],
y1_name='1 district', y2=results['main_district_system']['mean_set'],
std2=results['main_district_system']['std_set'], y2_name=f'{config.q} districts',
quantity='media', suffix=suffix, xlab='media influence', ylab=f'election result of {media_state}',
ylim=ylim_mean, save_file=True)

plot_mean_std_all(voting_systems=config.voting_systems.keys(), x=media_influence, results=results, quantity='media',
suffix=suffix, xlab='media influence', ylab=f'election result of {media_state}', ylim=ylim_mean,
save_file=True)


if __name__ == '__main__':
os.chdir(parentdir)
Expand Down
11 changes: 4 additions & 7 deletions scripts/zealot_susceptibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from configuration.parser import get_arguments
from tools import convert_to_distributions, split_suffix
from plotting import plot_mean_std, plot_heatmap, plot_std, plot_mean_per, plot_mean_diff, plot_mean_std_two_systems
from plotting import plot_mean_std, plot_heatmap, plot_std, plot_mean_per, plot_mean_diff, plot_mean_std_all

# parameters of simulations not present in the config module
zn_set = range(31) # range of considered number of zealots
Expand Down Expand Up @@ -78,12 +78,9 @@ def plot_zealot_susceptibility(config):
suffix=suffix, xlab='number of zealots', ylab=f'susceptibility derivative',
ylim=(), save_file=True)

plot_mean_std_two_systems(
x=zn_set, y1=results['countrywide_system']['mean_set'], std1=results['countrywide_system']['std_set'],
y1_name='1 district', y2=results['main_district_system']['mean_set'],
std2=results['main_district_system']['std_set'], y2_name=f'{config.q} districts',
quantity='zealots', suffix=suffix, xlab='number of zealots', ylab=f'election result of {config.zealot_state}',
ylim=ylim_mean, save_file=True)
plot_mean_std_all(voting_systems=config.voting_systems.keys(), x=zn_set, results=results, quantity='zealots',
suffix=suffix, xlab='number of zealots', ylab=f'election result of {config.zealot_state}',
ylim=ylim_mean, save_file=True)


if __name__ == '__main__':
Expand Down

0 comments on commit b6ce771

Please sign in to comment.