len(dir()) %pylab inline len(dir()) %pylab inline import mpl_toolkits.mplot3d t=linspace(-9,9,dtype='i') x,y=meshgrid(t,t) gca(projection='3d').plot_surface(x,y,x*x^y) #Jupyter %pylab inline import pandas as pd rnd=np.random.random t=arange(10) f=pd.DataFrame(dict(a=t, b=t+rnd(10), c=rnd(10))) subplot(221);plot(f.a,f.b,'.') subplot(222);plot(f.a,f.c,'mo:') subplot(223);plot(f.b,f.a,'^-') subplot(224);plot(f.b,f.c,'rv--') f #Jupyter #pandas #matplotlib %pylab inline import seaborn style.use('seaborn-poster') #pretty rnd = random.random t = arange(99) * .1 f = pd.DataFrame(dict(a=t, b=t+rnd(99), c=rnd(99)+rnd(99)+rnd(99))) f.boxplot() f.hist() f.plot() #Jupyter #pandas %pylab inline import seaborn style.use('seaborn-poster') import pandas as pd rnd = random.random u = arange(6) g = pd.DataFrame(dict(b=u+rnd(6), c=rnd(6)), index=list('ABCDEF')) print(g.b) print(g.ix['C']) xlim(0, 6) for r in g.itertuples(): text(r.b, r.c, r.Index) g list(g.b) g.index %%time #Jupyter %pylab inline import seaborn style.use('seaborn-poster') rc('figure', figsize=(13,10)) z = c = linspace(-2, 0.5, 1400) + linspace(-1j, 1j, 1400)[:, None] for i in range(32): z = z**2 + c imshow(abs(z) < 2, extent=[c.real.min(), c.real.max(), c.imag.min(), c.imag.max()]) c.imag (newaxis, newaxis) #Jupyter #DSP %pylab inline import seaborn style.use('seaborn-poster') #pretty x = linspace(0, 2*pi, 99) env = bartlett(len(x)) subplot(311) stem(x, sin(8*x)*env, 'b', 'bo') stem(x, cos(9*x)*env*2, 'r:', 'r.') #Jupyter #Python #matplotlib %pylab inline import seaborn style.use('seaborn-poster') #pretty rcParams['lines.linewidth'] = 0.5 x = arange(1, 64) f = x * (x-48)**2 - 4096 plot(x, f, label='$\phi$') plot(x, x * 100, label='$100x$') # 2nd func m = f.argmax() axvline(x[m]) text(x[m] + .5, f[m] + 300, 'max') n = f.argmin() axvline(x[n]) annotate('min', (x[n], f[n]), textcoords='offset pixels', xytext=(-100,0), arrowprops=dict(arrowstyle='->'), va='center') legend() gca().set_xscale('log') matshow(bitwise_and.outer(x, x), cmap=cm.seismic) # plot #2. seaborn default is grayscale colorbar() %pylab inline import seaborn style.use('seaborn-poster') import sympy sympy.init_printing() x = sympy.symbols('x') y = (x-1)*(x-2)*(x-5) d = y.diff().simplify() sympy.plotting.plot(y, d, d.integrate(), (x, -1, 7)) y, y.expand(), d, sympy.solve(d), y.integrate().factor() import sympy sympy.init_printing() x = sympy.symbols('x') (((x-16)*(x-48)).integrate()).factor() #Jupyter #Sympy #Numpy #matplotlib %pylab inline import seaborn style.use('seaborn-poster') import sympy sympy.init_printing() x = sympy.symbols('x') y = x*(x-4)*(x-3) - 3 yp = y.diff() ypp = yp.diff() # sympy.plotting can't axvline, so lambdify to plot with numpy yf, ypf, yppf = [sympy.lambdify(x, f, 'numpy') for f in [y, yp, ypp]] xs = linspace(0, 5) plot(xs, zip(yf(xs), ypf(xs), yppf(xs))) es = sympy.solve(yp) + sympy.solve(ypp) for e in es: axvline(e) y, yp, yp.simplify(), es plot(xs, stack((yf(xs), ypf(xs), yppf(xs)), -1)) #Jupyter #DSP #FFT #matplotlib #FM %pylab inline import seaborn style.use('seaborn-poster') # omit 6π sample, avoid leakage t = linspace(0, 6*pi, 193)[:-1] # FM synth, -60dB noise f = sin(t + cos(3*t)/4 + cos(2*t)/4) + random.random(len(t))/999 subplot(2, 1, 1); xlim(t.min(), t.max()); plot(t, f) # PSD subplot(2, 1, 2).set_yscale('log'); xticks(arange(0, 101, 3)); stem(abs(fft.fft(f))[:len(f)//2]**2) #Jupyter #matplotlib %pylab inline import seaborn style.use('seaborn-poster') # meshgrid alternative x = tile(linspace(-1, 1), [50, 1]) # make circles round gca().set_aspect(1) # use non-grayscale background; set_facecolor in matplotlib ≥2.0 # seaborn-poster grayscale colormap is murder for contour plots gca().set_axis_bgcolor('#3fcfef') # paraboloid contour(x, x.T, x**2 + x.T**2) colorbar() #Jupyter #matplotlib %pylab inline import seaborn style.use('seaborn-poster') f = random.random((6, 99)).sum(axis=0) subplot(411); tick_params(labelbottom=False); h = hist(f, 20) subplot(412, sharex=gca()); tick_params(labelbottom=False, labelleft=False); boxplot(f, vert=False) subplot(212, sharex=gca()); plot(h[1][1:], h[0].cumsum(), 'wo-'); ylabel('cdf') f.sort(); plot(f, arange(len(f)), ':') xlim(.5, 5.5) h #Jupyter #matplotlib #SVG %pylab inline import mpl_toolkits.mplot3d from scipy.signal import convolve2d import seaborn style.use('seaborn-poster') %config InlineBackend.figure_format = 'svg' # or 'pdf'; default 'png' x = linspace(-10, 10, 20) y = x[:, None] rs = x**2 + y**2 f = sin(rs*6)/rs w = random.random((60, 60)) > .996 g = convolve2d(f, w) gx, gy = indices(g.shape) gca(projection='3d').contour(gx, gy, g) t = linspace(-1, 1, 99) plot(30*(1+sin(9*t)*t), 30*(1+cos(9*t)*t), t) matshow(g) from scipy.signal import convolve2d m = zeros((60, 60)) m[random.randint(0, m.shape[0], 10), random.randint(0, m.shape[0], 10)] = random.random(10) - .5 matshow(convolve2d(m, f))