This requires you to not use inline mode. To do this, launch IPython from the terminal with the command
ipython notebook --pylab
In [1]:
import numpy as np
import matplotlib.pyplot as plt
# for normal distribution
from scipy.stats import norm
The following allows latex rendering of text
In [2]:
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)
In [2]:
In [2]:
In [3]:
N=1000
muS=1
muN=0
sigma=1
SNx1 = np.random.normal(muS, sigma, 1000)
SNx2 = np.random.normal(muN, sigma, 1000)
NSx1 = np.random.normal(muN, sigma, 1000)
NSx2 = np.random.normal(muS, sigma, 1000)
In [123]:
clf()
f = plt.figure(num=1, figsize=(8, 6), dpi=600, facecolor='none', edgecolor='k')
In [124]:
ax1=plt.subplot(1,2,1, aspect='equal',axisbg='none')
plt.rc('text', usetex=True)
plt.scatter(SNx1, SNx2, s=15, c=[1,0.3,0.3], alpha=0.4, edgecolors='none')
plt.scatter(NSx1, NSx2, s=15, c=[0.3,0.3,1], alpha=0.4, edgecolors='none')
padding=sigma*3
axis([muN-padding,muS+padding,muN-padding,muS+padding])
ax1.tick_params(direction='out')
# Move left and bottom spines outward by 10 points
#ax1.spines['left'].set_position(('outward', 10))
#ax1.spines['bottom'].set_position(('outward', 10))
# Hide the right and top spines
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
# Only show ticks on the left and bottom spines
ax1.yaxis.set_ticks_position('left')
ax1.xaxis.set_ticks_position('bottom')
xlabel('$x_1$')
ylabel('$x_2$')
ax1.set_xticks([0,muS])
ax1.set_yticks([0,muS])
ax1.set_xticklabels(['$0$','$\mu_S$'])
ax1.set_yticklabels(['$0$','$\mu_S$'])
#plt.axis([-6, -6, 10, 10])
# plot decision line
plt.plot((-8,10),(-8,10),'k:')
# Plot distance between centres
plt.annotate ('', (muS,0), (0,muS), arrowprops={'arrowstyle':'<->'})
#plt.text( muS,muS, '$\sqrt{2\mu_S^2}$', rotation=45)
#plt.annotate(
# 'D = 1', xy=(1, 9), xycoords = 'data',
# xytext = (5, 0), textcoords = 'offset points')
plt.text( 3*muS,-2*muS, '$SN$')
plt.text( -2*muS,3*muS, '$NS$')
Out[124]:
In [125]:
ax2 = plt.subplot(1,2,2, aspect=20,axisbg='none')
plt.rc('text', usetex=True)
xmin=-muS-5
xmax=muS+5
# vector of x values
x=np.linspace(xmin,xmax,501)
# create a corresponding vector of zeros for use later
theFloor = np.zeros(dsn.size)
# Calculate the y-values of the distributions
dsn = norm.pdf(x,muS,sqrt(2)*sigma)
dns = norm.pdf(x,-muS,sqrt(2)*sigma)
maxy=np.max(dsn)
plt.plot( x, dns,'b-')
plt.plot( x, dsn,'r-')
# Fill error regions
ax2.fill_between(x, 0, dsn, where=x<=0, facecolor='r', alpha=0.5, edgecolor='none')
ax2.fill_between(x, 0, dns, where=x>=0, facecolor='b', alpha=0.5, edgecolor='none')
# Move left and bottom spines outward by 10 points
#ax.spines['left'].set_position(('outward', 10))
#ax.spines['bottom'].set_position(('outward', 10))
# Hide the right and top spines
ax2.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.spines['left'].set_visible(False)
# Only show ticks on the left and bottom spines
#ax2.yaxis.set_ticks_position('left')
ax2.xaxis.set_ticks_position('bottom')
ax2.tick_params(direction='out')
xlabel('$d = x_1-x_2$')
ax2.set_xticks([-muS,0,muS])
ax2.set_yticks([])
ax2.set_xticklabels(['$-\mu_S$','$0$','$\mu_S$'])
# draw decision boundary
plt.axvline(x=0, linestyle=':', color='k')
# distribution labels
plt.text( muS*2.7,maxy*0.5, '$d_{SN}$',
horizontalalignment='left',
color ='r')
plt.text( -muS*2.7,maxy*0.5, '$d_{NS} $',
horizontalalignment='right',
color ='b')
# Decision test
plt.text(0.5, maxy*1.05, 'say ${SN}$',
horizontalalignment='left')
plt.text(-0.5, maxy*1.05, 'say ${NS}$',
horizontalalignment='right')
Out[125]:
In [125]:
In [125]:
In [126]:
# save it
plt.savefig('testAFCplot.pdf',dpi=300, bbox_inches='tight', transparent=True)
In [122]:
In [129]:
?fill_between
In [ ]: