PDF link: Graphing and Inkscape

Loader Loading...
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab
# Showing significance between groups in Python with matplotlib and seaborn

import pandas as pd
import numpy as np
import scipy.stats as sci
import pickle
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
import seaborn as sns


data = pd.DataFrame(np.random.random_sample((60,1)),columns = ['measure'])
data['treatment'] = '';
data['treatment'][0:20] = 'Control';
data['treatment'][20:40] = 'Treatment 1';
data['treatment'][40:60] = 'Treatment 2';

data['measure'][0:20] = data['measure'][0:20]+2;
data['measure'][20:40] = data['measure'][20:40]+5;
data['measure'][40:60] = data['measure'][40:60]+6;


plt.figure()
g = sns.violinplot(x='treatment',y='measure',data=data)
ax = g.axes
ax.set_ylim(0,10)
ax.set_ylabel('Firing Rate (Hz)')
ax.set_xlabel('')

plt.plot([0,0,2,2],[4, 9, 9, 7.7],linewidth=1,color='k')
plt.text(0.92,9.2,'**',fontsize=16);


plt.plot([0,0,1,1],[4, 7, 7, 6.5],linewidth=1,color='k')
plt.text(0.5,7.2,'*',fontsize=16);

plt.plot([1,1,2,2],[6.5,8 ,8 ,7.5],linewidth=1,color='k')
plt.text(1.5,8.2,'*',fontsize=16);

data = pd.DataFrame(np.random.random_sample((100,1)),columns = ['measure'])
data['treatment'] = '';
data['treatment'][0:20] = 'Control';
data['treatment'][20:40] = 'Treatment 1';
data['treatment'][40:60] = 'Treatment 2';
data['treatment'][60:80] = 'Treatment 3';
data['treatment'][80:100] = 'Treatment 4';

data['measure'][0:20] = data['measure'][0:20]+2;
data['measure'][20:40] = data['measure'][20:40]+5;
data['measure'][40:60] = data['measure'][40:60]+6;
data['measure'][60:80] = data['measure'][60:80]+7;
data['measure'][80:100] = data['measure'][80:100]+9;

sns.set_palette("Blues");

plt.figure()
g = sns.violinplot(x='treatment',y='measure',data=data)
ax = g.axes
ax.set_ylim(0,12)
ax.set_ylabel('Firing Rate (Hz)')
ax.set_xlabel('')

plt.text(-.06,4,'a',fontsize=16);
plt.text(0.94,7.5,'b',fontsize=16);
plt.text(1.86,8.25,'bc',fontsize=16);
plt.text(2.86,9,'bc',fontsize=16);
plt.text(3.94,10.8,'c',fontsize=16);

plt.plot([0,0,1,1],[4, 7, 7, 6.8],linewidth=1,color='k')
plt.text(0.5,7,'*',fontsize=16);

plt.plot([0,0,2,2],[4, 8, 8,7.8],linewidth=1,color='k')
plt.text(1,8,'*',fontsize=16);

plt.plot([0,0,3,3],[4, 8.75, 8.75, 8.5],linewidth=1,color='k')
plt.text(1.5,8.75,'*',fontsize=16);

plt.plot([0,0,4,4],[4, 11, 11, 10.8],linewidth=1,color='k')
plt.text(2,11,'*',fontsize=16);

plt.plot([1,1,4,4],[6.8, 10.5, 10.5, 10.8],linewidth=1,color='k')
plt.text(2,10.5,'*',fontsize=16);
%% Error bar examples in matlab

a = rand(20,1);
a = a+2.5;
b = rand(20,1);
b = b+1;
b=b*3;

c = mean(a);
d = mean(b);

x1(1:20) = 1;
x2(1:20) = 2;

bar([c d]) % ew

%% standard deviation with bar
figure('Renderer', 'painters', 'Position', [10 10 600 600])
hold on
bar([c d],'edgecolor','k','facecolor','w');

s = scatter(x1,a,'filled','k', 'jitter','on', 'jitterAmount',0.2);
s.MarkerFaceAlpha = 0.3;
s = scatter(x2,b,'filled','k', 'jitter','on', 'jitterAmount',0.2);
s.MarkerFaceAlpha = 0.3;

errorbar(1,c,std(a),'-k','linewidth',1.5,'capsize',5);
errorbar(2,d,std(b),'-k','linewidth',1.5,'capsize',5);
hold off
ylim([0 max(b)+std(b)])

%% standard deviation no bar
figure('Renderer', 'painters', 'Position', [10 10 600 600])
hold on

s = scatter(x1,a,'filled','k', 'jitter','on', 'jitterAmount',0.2);
s.MarkerFaceAlpha = 0.3;
s = scatter(x2,b,'filled','k', 'jitter','on', 'jitterAmount',0.2);
s.MarkerFaceAlpha = 0.3;

errorbar(1,c,std(a),'-ok','linewidth',2,'capsize',7);
errorbar(2,d,std(b),'-ok','linewidth',2,'capsize',7);
hold off
ylim([0 max(b)+std(b)])
xlim([0 3])

%% standard error
figure('Renderer', 'painters', 'Position', [10 10 600 600])
hold on

s = scatter(x1,a,'filled','k', 'jitter','on', 'jitterAmount',0.2);
s.MarkerFaceAlpha = 0.3;
s = scatter(x2,b,'filled','k', 'jitter','on', 'jitterAmount',0.2);
s.MarkerFaceAlpha = 0.3;

errorbar(1,c,std(a)/sqrt(20),'-ok','linewidth',2,'capsize',7);
errorbar(2,d,std(b)/sqrt(20),'-ok','linewidth',2,'capsize',7);
hold off
ylim([0 max(b)+std(b)])
xlim([0 3])

%% Confidence interval 

dat = [a b];
ci = 0.95;
alpha = 1 - ci;

n = size(dat,1);
T_multi = tinv(1-alpha/2,n-1);
ci95 = T_multi*std(dat)/sqrt(n);

figure('Renderer', 'painters', 'Position', [10 10 600 600])
hold on

s = scatter(x1,a,'filled','k', 'jitter','on', 'jitterAmount',0.2);
s.MarkerFaceAlpha = 0.4;
s = scatter(x2,b,'filled','k', 'jitter','on', 'jitterAmount',0.2);
s.MarkerFaceAlpha = 0.4;

errorbar(1,c,ci95(1),ci95(1),'-k','linewidth',2,'capsize',7);
errorbar(2,d,ci95(2),ci95(2),'-k','linewidth',2,'capsize',7);

line([0.9 1.1],[c c],'color','k','linewidth',3)
line([1.9 2.1],[d d],'color','k','linewidth',3)

hold off
ylim([0 max(b)+std(b)])
set(gca,'Xtick',0:3)
xticklabels({'' 'Ctrl' 'Treat' ''})
xlim([0 3])