Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 534bf31

Browse files
committed
Merge pull request #38 from phobson/generic-plotting
Generic plotting function
2 parents 765fe60 + 7c2fc60 commit 534bf31

17 files changed

Lines changed: 149 additions & 61 deletions

docs/tutorial/activities.ipynb

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
"cell_type": "code",
2727
"execution_count": null,
2828
"metadata": {
29-
"collapsed": true
29+
"collapsed": false
3030
},
3131
"outputs": [],
3232
"source": [
3333
"import warnings\n",
3434
"warnings.simplefilter('ignore')\n",
3535
"\n",
3636
"from numpy.random import seed\n",
37+
"from scipy import stats\n",
3738
"from matplotlib import pyplot\n",
3839
"import seaborn\n",
3940
"\n",
@@ -127,7 +128,7 @@
127128
"cell_type": "markdown",
128129
"metadata": {},
129130
"source": [
130-
"### Fitting distribution\n",
131+
"### Fitting distributions\n",
131132
"Fitting distributions to data follows a similar pattern."
132133
]
133134
},
@@ -143,6 +144,25 @@
143144
"paramnormal.activity.fit('beta', data)"
144145
]
145146
},
147+
{
148+
"cell_type": "markdown",
149+
"metadata": {},
150+
"source": [
151+
"Equivalent command to perform the same fits in raw scipy is shown below:"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"metadata": {
158+
"collapsed": false
159+
},
160+
"outputs": [],
161+
"source": [
162+
"# constrained loc and scale\n",
163+
"stats.beta.fit(data, floc=0, fscale=1)"
164+
]
165+
},
146166
{
147167
"cell_type": "markdown",
148168
"metadata": {},
@@ -165,8 +185,7 @@
165185
"cell_type": "markdown",
166186
"metadata": {},
167187
"source": [
168-
"### Plotting\n",
169-
"There is very limited plotting functionality built into paramnormal. For the time being, we just plot the probability distribution function (PDF), and offload everything else to the seaborn module if it's available."
188+
"And again in raw scipy:"
170189
]
171190
},
172191
{
@@ -177,15 +196,17 @@
177196
},
178197
"outputs": [],
179198
"source": [
180-
"ax = paramnormal.activity.plot('beta', α=3, β=2)\n",
181-
"ax.figure"
199+
"# constrained beta and scale, unconstrained loc\n",
200+
"stats.beta.fit(data, f1=2, fscale=1)"
182201
]
183202
},
184203
{
185204
"cell_type": "markdown",
186205
"metadata": {},
187206
"source": [
188-
"You can plot on an existing figure through the `ax` argument and control the line style through `line_opts`."
207+
"### Plotting\n",
208+
"There is very limited plotting functionality built into paramnormal.\n",
209+
"The probability distribution function (PDF) is plotted by default, but any other method of the distributions can be plotted by specifying the ``which`` parameters."
189210
]
190211
},
191212
{
@@ -196,21 +217,16 @@
196217
},
197218
"outputs": [],
198219
"source": [
199-
"seaborn.set(style='ticks', rc=clean_bkgd)\n",
200-
"fig, (ax1, ax2) = pyplot.subplots(nrows=2, sharex=True, sharey=True)\n",
201-
"ax1 = paramnormal.activity.plot('beta', α=6, β=2, ax=ax1, line_opts=dict(color='firebrick', lw=3))\n",
202-
"ax2 = paramnormal.activity.plot('beta', α=2, β=6, ax=ax2, line_opts=dict(color='forestgreen', lw=1.25))\n",
203-
"ax1.set_ylabel('α=6, β=2')\n",
204-
"ax2.set_ylabel('α=2, β=6')\n",
205-
"seaborn.despine(fig)\n",
206-
"fig"
220+
"ax = paramnormal.activity.plot('beta', α=3, β=2)\n",
221+
"paramnormal.activity.plot('beta', α=3, β=2, ax=ax, which='CDF')\n",
222+
"ax.legend()\n"
207223
]
208224
},
209225
{
210226
"cell_type": "markdown",
211227
"metadata": {},
212228
"source": [
213-
"Of course, you can create a fully-specified distribtion and omit the distribution parameters."
229+
"You can plot on an existing figure through the `ax` argument and control the line style through `line_opts`."
214230
]
215231
},
216232
{
@@ -221,16 +237,19 @@
221237
},
222238
"outputs": [],
223239
"source": [
224-
"beta = paramnormal.beta(α=3, β=2)\n",
225-
"ax = paramnormal.activity.plot(beta)\n",
226-
"ax.figure"
240+
"fig, (ax, ax2) = pyplot.subplots(nrows=2, sharex=True, sharey=True)\n",
241+
"paramnormal.activity.plot('beta', α=6, β=2, ax=ax, line_opts=dict(color='firebrick', lw=3))\n",
242+
"paramnormal.activity.plot('beta', α=2, β=6, ax=ax2, line_opts=dict(color='forestgreen', lw=1.25))\n",
243+
"ax.set_ylabel('α=6, β=2')\n",
244+
"ax2.set_ylabel('α=2, β=6')\n",
245+
"seaborn.despine(fig)"
227246
]
228247
},
229248
{
230249
"cell_type": "markdown",
231250
"metadata": {},
232251
"source": [
233-
"Passing an array of sample data will fit the distribution to that data and allow you to use `seaborn.distplot`."
252+
"Of course, you can create a fully-specified distribtion and omit the distribution parameters."
234253
]
235254
},
236255
{
@@ -241,18 +260,15 @@
241260
},
242261
"outputs": [],
243262
"source": [
244-
"data = paramnormal.activity.random('beta', α=3, β=2, shape=125)\n",
245-
"ax = paramnormal.activity.plot('beta', data, distplot=True)\n",
246-
"seaborn.despine()\n",
247-
"ax.legend()\n",
248-
"ax.figure"
263+
"beta = paramnormal.beta(α=3, β=2)\n",
264+
"ax = paramnormal.activity.plot(beta)"
249265
]
250266
},
251267
{
252268
"cell_type": "markdown",
253269
"metadata": {},
254270
"source": [
255-
"And you can control `seaborn.distplot` through `displot_opts`."
271+
"And finally, you can pass an array of data and an unfrozen distribution, and a new distribution will be fit to your data."
256272
]
257273
},
258274
{
@@ -263,12 +279,11 @@
263279
},
264280
"outputs": [],
265281
"source": [
266-
"data = paramnormal.activity.random('normal', μ=0.75, σ=1.25, shape=125)\n",
267-
"ax = paramnormal.activity.plot('normal', data, distplot=True,\n",
268-
" distplot_opts=dict(hist=True, rug=True, kde=False, norm_hist=True))\n",
269-
"seaborn.despine()\n",
270-
"ax.legend()\n",
271-
"ax.figure"
282+
"data = paramnormal.activity.random('beta', α=2, β=6, shape=37) + \\\n",
283+
" paramnormal.activity.random('normal', μ=5, σ=1, shape=37)\n",
284+
"ax = paramnormal.activity.plot('normal', data=data, line_opts=dict(label='Emperical Fit'))\n",
285+
"ax = paramnormal.activity.plot('normal', μ=5, σ=1, line_opts=dict(label='Theoretical'))\n",
286+
"ax.legend()"
272287
]
273288
}
274289
],
@@ -293,4 +308,4 @@
293308
},
294309
"nbformat": 4,
295310
"nbformat_minor": 0
296-
}
311+
}

paramnormal/activity.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def _check_distro(distro, **params):
2727

2828
def _check_ax(ax):
2929
if ax is None:
30-
fig, ax = pyplot.subplots()
31-
else:
3230
ax = pyplot.gca()
3331
fig = ax.figure
32+
else:
33+
fig = ax.figure
3434

3535
return fig, ax
3636

@@ -121,22 +121,8 @@ def fit(distro, data, as_params=True, **guesses):
121121
return distro.from_params(params)
122122

123123

124-
def _plot_pdf(distro, xlimits, ax=None, xscale='linear', **line_opts):
125-
fig, ax = _check_ax(ax)
126-
if xscale == 'log':
127-
xlimits = numpy.log10(xlimits)
128-
spacer = numpy.logspace
129-
else:
130-
spacer = numpy.linspace
131-
132-
x_hat = spacer(xlimits[0], xlimits[1], num=100)
133-
y_hat = distro.pdf(x_hat)
134-
ax.plot(x_hat, y_hat, **line_opts)
135-
return ax
136-
137-
138-
def plot(distro, data=None, fit_dist=True, ax=None, pad=0.1,
139-
xscale='linear', line_opts=None, **guesses):
124+
def plot(distro, which='PDF', data=None, fit_dist=True, ax=None,
125+
pad=0.05, xscale='linear', line_opts=None, **guesses):
140126
"""
141127
Plot the PDF of a dataset and other representations of the
142128
distribution (histogram, kernel density estimate, and rug plot).
@@ -253,18 +239,31 @@ def plot(distro, data=None, fit_dist=True, ax=None, pad=0.1,
253239
254240
"""
255241

242+
# validate the axes and distribution function (`which`)
243+
fig, ax = _check_ax(ax)
256244
if data is not None:
257245
distro = fit(distro, data, as_params=False, **guesses)
258246
else:
259247
distro = _check_distro(distro, **guesses)
248+
fxn = getattr(distro, which.lower())
260249

261-
fig, ax = _check_ax(ax)
250+
# determine and set the xlimits of the plot
251+
xlimits = distro.ppf([pad/100, 1 - pad/100])
252+
253+
# determine the x-values
254+
if xscale == 'log':
255+
#xlimits = numpy.log10(xlimits)
256+
x_hat = numpy.logspace(*numpy.log10(xlimits), num=100)
257+
else:
258+
x_hat = numpy.linspace(*xlimits, num=100)
259+
260+
# compute y-values
261+
y_hat = fxn(x_hat)
262262

263263
line_opts = dict() if line_opts is None else line_opts
264-
line_opts['label'] = line_opts.pop('label', 'PDF')
265-
xlimits = distro.ppf([pad/100, 1 - pad/100])
264+
line_opts['label'] = line_opts.pop('label', which)
266265

267-
ax = _plot_pdf(distro, xlimits, ax=ax, xscale=xscale, **line_opts)
266+
line, = ax.plot(x_hat, y_hat, **line_opts)
268267
ax.set_xscale(xscale)
269268

270269
return ax
Binary file not shown.
20.1 KB
Loading
30.2 KB
Loading
19 KB
Loading
Binary file not shown.
Binary file not shown.
-44.2 KB
Binary file not shown.
28.8 KB
Loading

0 commit comments

Comments
 (0)