Skip to content

Instantly share code, notes, and snippets.

@msund
Created May 1, 2014 02:53
Show Gist options
  • Select an option

  • Save msund/6e1626ab57cb59f61048 to your computer and use it in GitHub Desktop.

Select an option

Save msund/6e1626ab57cb59f61048 to your computer and use it in GitHub Desktop.
Test
{
"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": "Populating the interactive namespace from numpy and matplotlib\n"
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": "from matplotlylib import fig_to_plotly\nusername = 'IPython.Demo'\napi_key = '1fw3zw2o13'",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"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/3206/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": "<IPython.core.display.HTML at 0x10bd03990>"
}
],
"prompt_number": 3
},
{
"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/3209/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": "<IPython.core.display.HTML at 0x10c0c7cd0>"
}
],
"prompt_number": 6
},
{
"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/3210/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": "<IPython.core.display.HTML at 0x10c0fcd10>"
}
],
"prompt_number": 7
},
{
"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/3211/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": "<IPython.core.display.HTML at 0x10c2121d0>"
}
],
"prompt_number": 8
},
{
"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),',')\n\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/3212/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": "<IPython.core.display.HTML at 0x10c265650>"
}
],
"prompt_number": 9
},
{
"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": 10
},
{
"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": [
{
"output_type": "stream",
"stream": "stderr",
"text": "/Users/matthewsundquist/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlylib/renderer.py:363: UserWarning: Bummer! Plotly can currently only draw Line2D objects from matplotlib that are in 'data' coordinates!\n warnings.warn(\"Bummer! Plotly can currently only draw Line2D \"\n"
},
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/3213/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": "<IPython.core.display.HTML at 0x10c2486d0>"
}
],
"prompt_number": 11
},
{
"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/3214/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": "<IPython.core.display.HTML at 0x10c3d0dd0>"
}
],
"prompt_number": 12
},
{
"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/3215/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": "<IPython.core.display.HTML at 0x10c26bbd0>"
}
],
"prompt_number": 13
},
{
"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/3216/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": "<IPython.core.display.HTML at 0x10cc98450>"
}
],
"prompt_number": 14
},
{
"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/3217/600/450\" width=\"650\"></iframe>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 15,
"text": "<IPython.core.display.HTML at 0x10d93b310>"
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment