Skip to content

Instantly share code, notes, and snippets.

@jepio
Last active August 29, 2015 14:05
Show Gist options
  • Save jepio/48da1fd5de88e33656d3 to your computer and use it in GitHub Desktop.
Save jepio/48da1fd5de88e33656d3 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.stats import norm
a = np.array([[1, 3, 5], [2, 4, 6]])
## keyword args can be in any order
xdata = np.array(a, order='F', dtype=np.float32, ndmin=3) # fortran data order
xdata.resize(6)
print xdata
ydata = norm.pdf(xdata, loc=2.5)
f = lambda x, p0, p1, p2: p0 * (x - p1) ** 2 + p2
par_values, par_cov = curve_fit(f, xdata, ydata)
print par_values
style = {'color': 'blue', 'linestyle': 'dashed', 'marker':
'o', 'markerfacecolor': 'blue', 'markersize': 6}
## single star expands a list/tuple into positional args
plt.plot(xdata, f(xdata, *par_values), label='Quadratic fit', color='red')
## double start expands a dictionary into keyword args, such as color='green'
plt.plot(xdata, ydata, label='Data', **style)
plt.legend()
plt.show()
### From xaratustrah
# create a mask
mask = np.where(np.logical_and(t>10, t<12), 1.0, 0.0)
# apply
s2 = s2 * mask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment