Skip to content

Instantly share code, notes, and snippets.

@msund
Created March 31, 2014 18:43
Show Gist options
  • Save msund/9899250 to your computer and use it in GitHub Desktop.
Save msund/9899250 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "Science NBs"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "%pylab inline\nimport matplotlib.pyplot as plt # so we don't have to look at mpl's backend\nimport matplotlib.gridspec as gridspec # for subplots\nimport matplotlib.cm as cm # for fun-with-colors\nimport numpy as np",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "\nWelcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].\nFor more information, type 'help(pylab)'.\n"
}
],
"prompt_number": 56
},
{
"cell_type": "code",
"collapsed": false,
"input": "from matplotlylib import fig_to_plotly\nusername = 'IPython.Demo'\napi_key = '1fw3zw2o13'",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 59
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig1 = plt.figure()\nfrom random import gauss\ngrands = []\nfor i in range(100):\n grands.append(gauss(0,1))\nplot(grands)\nfig_to_plotly(fig1, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2417/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 66,
"text": "<IPython.core.display.HTML at 0x110c94ed0>"
}
],
"prompt_number": 66
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig2 = plt.figure()\nplot(rand(100))\nfig_to_plotly(fig2, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2418/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 67,
"text": "<IPython.core.display.HTML at 0x1103ec5d0>"
}
],
"prompt_number": 67
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig3 = plt.figure()\nnpts = 5000\nxs = 2*rand(npts)-1\nys = 2*rand(npts)-1\nr = xs**2+ys**2\nninside = (r<1).sum()\nfigsize(6,6) # make the figure square\ntitle(\"Approximation to pi = %f\" % (4*ninside/float(npts)))\nplot(xs[r<1],ys[r<1],'b.')\nplot(xs[r>1],ys[r>1],'r.')\nfigsize(8,6) # change the figsize back to 4x3 for the rest of the notebook\nfig_to_plotly(fig3, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2419/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 68,
"text": "<IPython.core.display.HTML at 0x110f5dd50>"
}
],
"prompt_number": 68
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig4 = plt.figure()\nplot (rand(100))\nfig_to_plotly(fig4, username, api_key, notebook = True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2421/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 70,
"text": "<IPython.core.display.HTML at 0x10fcad390>"
}
],
"prompt_number": 70
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig5 = plt.figure()\nfrom scipy.fftpack import fft,fftfreq\n\nnpts = 4000\nnplot = npts/10\nt = linspace(0,120,npts)\ndef acc(t): return 10*sin(2*pi*2.0*t) + 5*sin(2*pi*8.0*t) + 2*rand(npts)\n\nsignal = acc(t)\n\nFFT = abs(fft(signal))\nfreqs = fftfreq(npts, t[1]-t[0])\n\nsubplot(211)\nplot(t[:nplot], signal[:nplot])\nsubplot(212)\nplot(freqs,20*log10(FFT),',')\nshow()\nfig_to_plotly(fig5, username, api_key, notebook = True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2422/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 71,
"text": "<IPython.core.display.HTML at 0x10d35f290>"
}
],
"prompt_number": 71
},
{
"cell_type": "code",
"collapsed": false,
"input": "class Schrod1d:\n \"\"\"\\\n Schrod1d: Solver for the one-dimensional Schrodinger equation.\n \"\"\"\n def __init__(self,V,start=0,end=1,npts=50,**kwargs):\n m = kwargs.get('m',1.0)\n self.x = linspace(start,end,npts)\n self.Vx = V(self.x)\n self.H = (-0.5/m)*self.laplacian() + diag(self.Vx)\n return\n \n def plot(self,*args,**kwargs):\n titlestring = kwargs.get('titlestring',\"Eigenfunctions of the 1d Potential\")\n xstring = kwargs.get('xstring',\"Displacement (bohr)\")\n ystring = kwargs.get('ystring',\"Energy (hartree)\")\n if not args:\n args = [3]\n x = self.x\n E,U = eigh(self.H)\n h = x[1]-x[0]\n\n # Plot the Potential\n plot(x,self.Vx,color='k')\n\n for i in range(*args):\n # For each of the first few solutions, plot the energy level:\n axhline(y=E[i],color='k',ls=\":\")\n # as well as the eigenfunction, displaced by the energy level so they don't\n # all pile up on each other:\n plot(x,U[:,i]/sqrt(h)+E[i])\n title(titlestring)\n xlabel(xstring)\n ylabel(ystring) \n return\n \n def laplacian(self):\n x = self.x\n h = x[1]-x[0] # assume uniformly spaced points\n n = len(x)\n M = -2*identity(n,'d')\n for i in range(1,n):\n M[i,i-1] = M[i-1,i] = 1\n return M/h**2",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 44
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig6 = plt.figure()\nsquare_well = Schrod1d(lambda x: 0*x,m=10)\nsquare_well.plot(4,titlestring=\"Square Well Potential\")\nfig_to_plotly(fig6, username, api_key, notebook = True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2423/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 72,
"text": "<IPython.core.display.HTML at 0x110fb1590>"
}
],
"prompt_number": 72
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig7 = plt.figure()\nho = Schrod1d(lambda x: x**2,start=-3,end=3)\nho.plot(6,titlestring=\"Harmonic Oscillator\")\nfig_to_plotly(fig7, username, api_key, notebook = True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2424/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 73,
"text": "<IPython.core.display.HTML at 0x110fd41d0>"
}
],
"prompt_number": 73
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig8 = plt.figure()\ndef finite_well(x,V_left=1,V_well=0,V_right=1,d_left=10,d_well=10,d_right=10):\n V = zeros(x.size,'d')\n for i in range(x.size):\n if x[i] < d_left: \n V[i] = V_left\n elif x[i] > (d_left+d_well):\n V[i] = V_right\n else:\n V[i] = V_well\n return V\n \nfw = Schrod1d(finite_well,start=0,end=30,npts=100)\nfw.plot()\nfig_to_plotly(fig8, username, api_key, notebook = True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2425/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 74,
"text": "<IPython.core.display.HTML at 0x110bda610>"
}
],
"prompt_number": 74
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig9 = plt.figure()\ndef triangular(x,F=30): return F*x\n\ntw = Schrod1d(triangular,m=10)\ntw.plot()\nfig_to_plotly(fig9, username, api_key, notebook = True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2426/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 75,
"text": "<IPython.core.display.HTML at 0x10fce34d0>"
}
],
"prompt_number": 75
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig10 = plt.figure()\ndef tri_finite(x): return finite_well(x)+triangular(x,F=0.025)\n\ntfw = Schrod1d(tri_finite,start=0,end=30,npts=100)\ntfw.plot()\nfig_to_plotly(fig10, username, api_key, notebook = True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2427/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 76,
"text": "<IPython.core.display.HTML at 0x10fcc8390>"
}
],
"prompt_number": 76
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment