Skip to content

Instantly share code, notes, and snippets.

@fsantini
Created November 29, 2022 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsantini/30134dcf8d05276f7afd787b34e76c61 to your computer and use it in GitHub Desktop.
Save fsantini/30134dcf8d05276f7afd787b34e76c61 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 15:01:38 2022
@author: Francesco Santini
"""
current_frame = 0;
import numpy as np
from numpy.random import default_rng
from scipy.stats import ttest_ind
for seed in range(1,1000):
rng = default_rng(seed)
vals = rng.standard_normal(200)
vals1 = vals[:100]
vals2 = vals[100:]
t,p = ttest_ind(vals1, vals2)
print(p)
if p < 0.01:
break
import matplotlib.pyplot as plt
def fade_violins_out(violin_list, file_prefix, start_frame = 0):
start_body_alpha = 0.3
start_line_alpha = 1
nframes = 10
body_alphas = np.linspace(start_body_alpha, 0, nframes)
line_alphas = np.linspace(start_line_alpha, 0, nframes)
for frame in range(nframes):
for violin in violin_list:
for v_body in violin['bodies']:
v_body.set_alpha(body_alphas[frame])
orig_color = violin['cmeans'].get_color()
new_color = [*orig_color[0][:3], line_alphas[frame]]
violin['cmeans'].set_color(new_color)
plt.draw_all()
plt.savefig(f'{file_prefix}_{frame+start_frame:03d}.png', transparent=False)
def fade_violins_in(violin_list, file_prefix, start_frame = 0):
start_body_alpha = 0.3
start_line_alpha = 1
nframes = 10
body_alphas = np.linspace(0, start_body_alpha, nframes)
line_alphas = np.linspace(0, start_line_alpha, nframes)
for frame in range(nframes):
for violin in violin_list:
for v_body in violin['bodies']:
v_body.set_alpha(body_alphas[frame])
orig_color = violin['cmeans'].get_color()
new_color = [*orig_color[0][:3], line_alphas[frame]]
violin['cmeans'].set_color(new_color)
plt.draw_all()
plt.savefig(f'{file_prefix}_{frame+start_frame:03d}.png', transparent=False)
def fade_lines_in(line_list, file_prefix, start_frame = 0):
start_line_alpha = 1
nframes = 10
line_alphas = np.linspace(0, start_line_alpha, nframes)
for frame in range(nframes):
for line in line_list:
line.set_alpha(line_alphas[frame])
plt.draw_all()
plt.savefig(f'{file_prefix}_{frame+start_frame:03d}.png', transparent=False)
def fade_lines_out(line_list, file_prefix, start_frame = 0):
start_line_alpha = 1
nframes = 10
line_alphas = np.linspace(start_line_alpha, 0, nframes)
for frame in range(nframes):
for line in line_list:
line.set_alpha(line_alphas[frame])
plt.draw_all()
plt.savefig(f'{file_prefix}_{frame+start_frame:03d}.png', transparent=False)
def crossfade_lines(line_list_in, line_list_out, file_prefix, start_frame = 0):
start_line_alpha = 1
nframes = 10
line_alphas = np.linspace(0, start_line_alpha, nframes)
for frame in range(nframes):
for line in line_list_in:
line.set_alpha(line_alphas[frame])
for line in line_list_out:
line.set_alpha(1-line_alphas[frame])
plt.draw_all()
plt.savefig(f'{file_prefix}_{frame+start_frame:03d}.png', transparent=False)
def set_violin_color(violins, color):
for v_body in violins['bodies']:
v_body.set_color(color)
violins['cmeans'].set_color(color)
def plot_points(pts, location, color):
return plt.plot(np.ones(pts.shape)*location, pts, 'o', mfc=color, mec=color)
def move_points(lines, location):
orig_xdata = lines.get_xdata()
lines.set_xdata(np.ones(orig_xdata.shape)*location)
def animate_points(lines1, lines2, file_prefix, start_frame = 0):
nframes = 10;
start_location = 1;
end_location_1 = 0.5;
end_location_2 = 1.5;
locations_1 = np.linspace(start_location, end_location_1, nframes)
locations_2 = np.linspace(start_location, end_location_2, nframes)
for frame in range(nframes):
for lines in lines1:
move_points(lines, locations_1[frame])
for lines in lines2:
move_points(lines, locations_2[frame])
plt.draw_all()
plt.savefig(f'{file_prefix}_{frame+start_frame:03d}.png', transparent=False)
plt.close()
plt.axis('off')
plt.xlim(0,2)
violins = plt.violinplot(vals, showmeans=True, showextrema=False, widths = [0.5])
set_violin_color(violins, 'violet')
fade_violins_in([violins], 'test')
current_frame += 10
lines = plot_points(vals, 1, 'violet')
fade_lines_in(lines, 'test', current_frame)
current_frame += 10
fade_violins_out([violins], 'test', current_frame)
current_frame += 10
lines1 = plot_points(vals1, 1, 'red')
lines2 = plot_points(vals2, 1, 'blue')
crossfade_lines(lines1 + lines2, lines, 'test', current_frame)
current_frame += 10
animate_points(lines1, lines2, 'test', current_frame)
current_frame += 10
violin1 = plt.violinplot(vals1, positions=[0.5], showmeans=True, showextrema=False, widths = [0.5])
set_violin_color(violin1, 'red')
violin2 = plt.violinplot(vals2, positions=[1.5], showmeans=True, showextrema=False, widths = [0.5])
set_violin_color(violin2, 'blue')
fade_violins_in([violin1, violin2], 'test', current_frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment