[SciPy-dev] help with plot directive in scipy.stats.tutorial

josef.pktd at gmail.com josef.pktd at gmail.com
Tue Aug 25 16:39:23 EDT 2009


Since the stats.tutorial has now been merged into svn, I would like to
clean up the plot directives. When I wrote this initially, I didn't
realize that the plot commands do not have access to previously
defined variables.

I would like to replace the current code with plot directives using
script files instead of the inline plot directive. Because, I'm
currently not able or setup to build the docs, I would like to know if
this is ok or whether someone can test this.

The attached files are my original scripts, I haven't really reviewed
it just checked that they work.

Thanks,

Josef

.. plot:: examples/normdiscr_plot1.py
   :align: center
   :include-source: 0

.. plot:: examples/normdiscr_plot2.py
   :align: center
   :include-source: 0
-------------- next part --------------
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

npoints = 20 # number of integer support points of the distribution minus 1
npointsh = npoints/2
npointsf = float(npoints)
nbound = 4 #bounds for the truncated normal
normbound = (1+1/npointsf) * nbound #actual bounds of truncated normal
grid = np.arange(-npointsh,npointsh+2,1) #integer grid
gridlimitsnorm = (grid-0.5) / npointsh * nbound #bin limits for the truncnorm
gridlimits = grid - 0.5
grid = grid[:-1]
probs = np.diff(stats.truncnorm.cdf(gridlimitsnorm, -normbound, normbound))
gridint = grid
normdiscrete = stats.rv_discrete(values = (gridint,np.round(probs,decimals=7)),
                        name='normdiscrete')
   
   
n_sample = 500
np.random.seed(87655678) #fix the seed for replicability
rvs = normdiscrete.rvs(size=n_sample)
rvsnd=rvs
f,l = np.histogram(rvs,bins=gridlimits)
sfreq = np.vstack([gridint,f,probs*n_sample]).T
fs = sfreq[:,1] / float(n_sample)
ft = sfreq[:,2] / float(n_sample)
nd_std = np.sqrt(normdiscrete.stats(moments =  'v'))

ind = gridint  # the x locations for the groups
width = 0.35       # the width of the bars

plt.subplot(111)
rects1 = plt.bar(ind, ft, width, color='b')
rects2 = plt.bar(ind+width, fs, width, color='r')
normline = plt.plot(ind+width/2.0, stats.norm.pdf(ind,scale=nd_std), color='b')

plt.ylabel('Frequency')
plt.title('Frequency and Probability of normdiscrete')
plt.xticks(ind+width, ind )
plt.legend( (rects1[0], rects2[0]), ('true', 'sample') )

plt.show()
-------------- next part --------------
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

npoints = 20 # number of integer support points of the distribution minus 1
npointsh = npoints/2
npointsf = float(npoints)
nbound = 4 #bounds for the truncated normal
normbound = (1+1/npointsf)*nbound #actual bounds of truncated normal
grid = np.arange(-npointsh,npointsh+2,1) #integer grid
gridlimitsnorm = (grid-0.5) / npointsh * nbound #bin limits for the truncnorm
gridlimits = grid - 0.5
grid = grid[:-1]
probs = np.diff(stats.truncnorm.cdf(gridlimitsnorm, -normbound, normbound))
gridint = grid
normdiscrete = stats.rv_discrete(values = (gridint,np.round(probs,decimals=7)),
                        name='normdiscrete')
   
n_sample = 500
np.random.seed(87655678) #fix the seed for replicability
rvs = normdiscrete.rvs(size=n_sample)
rvsnd=rvs
f,l = np.histogram(rvs,bins=gridlimits)
sfreq = np.vstack([gridint,f,probs*n_sample]).T
fs = sfreq[:,1]/float(n_sample)
ft = sfreq[:,2]/float(n_sample)
fs = sfreq[:,1].cumsum()/float(n_sample)
ft = sfreq[:,2].cumsum()/float(n_sample)
nd_std = np.sqrt(normdiscrete.stats(moments =  'v'))


ind = gridint  # the x locations for the groups
width = 0.35   # the width of the bars

plt.figure()
plt.subplot(111)
rects1 = plt.bar(ind, ft, width, color='b')
rects2 = plt.bar(ind+width, fs, width, color='r')
normline = plt.plot(ind+width/2.0, stats.norm.cdf(ind+0.5,scale=nd_std), color='b')

plt.ylabel('cdf')
plt.title('Cumulative Frequency and CDF of normdiscrete')
plt.xticks(ind+width, ind )
plt.legend( (rects1[0], rects2[0]), ('true', 'sample') )

plt.show()


More information about the SciPy-Dev mailing list