{ "metadata": { "name": "", "signature": "sha256:b511ce445c13732cfe38fea879da71aa5f699ef417b543aa420ef299315a9838" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Contour Plot Examples" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "an IPython Notebook by plotly" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check plotly version and sign in using the your username and API key:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly\n", "plotly.__version__" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "'0.5.13'" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "py = plotly.plotly(\"\", \"\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "A 2D contour histograms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following cell has the code used to generate this plot which features a 2d contour histogram (new!), one x histogram, one y histogram and some scatter points." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np # we'll need this\n", "\n", "# some linear space\n", "t = np.linspace(-1,1.2,2001)\n", "\n", "# cubic function plus norm. dist. noise\n", "x = (t**3)+(0.3*np.random.randn(2001))\n", "\n", "# sixth power plus norm. dist. noise\n", "y = (t**6)+(0.3*np.random.randn(2001))\n", "\n", "# dictionary for the scatter points\n", "scatter = {'x':x,\n", " 'y':y,\n", " 'mode':'markers',\n", " 'name':'points',\n", " 'marker':{'color':'rgb(102,0,0)',\n", " 'opacity':0.4,'size':2}}\n", "\n", "# dictionary for the 2d contour histogram\n", "hist2d = {'x':x,\n", " 'y':y,\n", " 'type':'histogram2dcontour', # NEw plot type!\n", " 'name':'density',\n", " 'scl':'Hot', # choose a color scale\n", " 'reversescl':True, # reverse to scale\n", " 'showscale':False, # don't show the color bar\n", " 'ncontours':20} # number of contours \n", "\n", "# dictionary for the x histogram\n", "histx = {'x':x,\n", " 'type':'histogramx',\n", " 'name':'x density',\n", " 'yaxis':'y2', # plot on yaxis2\n", " 'marker':{'color':'rgb(102,0,0)'}}\n", "\n", "# dictionary for th y histogram \n", "histy = {'y':y,\n", " 'type':'histogramy',\n", " 'name':'y density',\n", " 'xaxis':'x2', # plot on xaxis2\n", " 'bardir':'h', # horizontal bars!\n", " 'marker':{'color':'rgb(102,0,0)'}}\n", "\n", "# make data list out of the 4 trace dictionaries\n", "data = [scatter,hist2d,histx,histy]\n", "\n", "# some layout options \n", "layout = {'hovermode':'closest', # info about data pt closest to cursor will appear\n", " 'width':600,\n", " 'height':550,\n", " 'autosize':False,\n", " 'showlegend':False,\n", " 'bargap':0, # no space between bars\n", " 'margin':{'t':50},\n", " 'xaxis':{'domain':[0,0.85],'showgrid':False,'zeroline':False}, # remove grid\n", " 'yaxis':{'domain':[0,0.85],'showgrid':False,'zeroline':False}, # and thick\n", " 'xaxis2':{'domain':[0.85,1],'showgrid':False,'zeroline':False}, # zero on\n", " 'yaxis2':{'domain':[0.85,1],'showgrid':False,'zeroline':False}} # all axes\n", "\n", "# send data to Plotly and embed plot in notebook\n", "py.iplot(data, layout=layout, filename='alex-309')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "A contour plot of a vortex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now the code used to generate this contour plot." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# select size of square grid\n", "size = 100\n", "\n", "# linear space from -2 pi to 2 pi\n", "x = np.linspace(-2*np.pi,2*np.pi,size)\n", "\n", "# init. vortex surface\n", "z = np.empty((size,size))\n", "\n", "# compute vortex surface\n", "for i,xi in enumerate(x):\n", " r2 = (xi**2+x**2)\n", " z[i] = np.sin(xi)*np.cos(x)*np.sin(r2)/(np.log(r2+1))\n", " \n", "# make data list\n", "data = [{'x':x,\n", " 'y':x,\n", " 'z':z,\n", " 'type':'contour'}]\n", "\n", "# some layout options\n", "layout = {'width':600,\n", " 'height':550,\n", " 'margin':{'t':50},\n", " 'autosize':False}\n", "\n", "# send data to Plotly and embed plot in notebook\n", "py.iplot(data, layout=layout, filename='alex-310')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "
\n", " \"Plotly \n", "
\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", "\n", "

Got Questions and Feedback
about plotly?

\n", "\n", "
\n", "\n", "* email: feedback@plot.ly \n", "* tweet: \n", "@plotlygraphs" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# CSS styling within IPython notebook\n", "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"plotly-python-doc.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 } ], "metadata": {} } ] }