{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Parametric equations in plotly"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"Here are some possibilies:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# some parametric eqution\n",
"def F(t, R=1):\n",
" return R*np.cos(t), R*np.sin(t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Show parameter on hover"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t = np.linspace(0, 2*np.pi, 100)\n",
"x, y = F(t)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trace = dict(\n",
" x=x,\n",
" y=y,\n",
" text=t\n",
")\n",
"\n",
"layout = dict(\n",
" autosize=False,\n",
" width=500,\n",
" height=500,\n",
" hovermode='closest'\n",
")\n",
"\n",
"fig = dict(data=[trace], layout=layout)\n",
"\n",
"py.iplot(fig, filename='parametric-curve')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Delimit parametric curve with condition"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def condition(xi, yi):\n",
" return xi * yi >= 0 # or some condition in the complex plane\n",
"\n",
"def separate_traces_by_condition(condition, x, y):\n",
" data = []\n",
" state = condition(x[0], y[0])\n",
" trace = dict(x=[x[0]], y=[y[0]])\n",
" \n",
" for xi, yi in zip(x, y)[1:]:\n",
" state_at_i = condition(xi, yi)\n",
" if state is state_at_i:\n",
" trace['x'].append(xi)\n",
" trace['y'].append(yi)\n",
" else:\n",
" data.append(trace)\n",
" trace = dict(x=[], y=[])\n",
" state = state_at_i\n",
"\n",
" data.append(trace)\n",
" return data\n",
" \n",
"t = np.linspace(0, 2*np.pi, 1000)\n",
"x, y = F(t)\n",
"data = separate_traces_by_condition(condition, x, y)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(data) # 4 traces, each with a different color"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layout = dict(\n",
" autosize=False,\n",
" width=500,\n",
" height=500,\n",
" hovermode='closest'\n",
")\n",
"\n",
"fig = dict(data=data, layout=layout)\n",
"\n",
"py.iplot(fig, filename='parametric-curve-2')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot points with a colorscale"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t = np.linspace(0, 2*np.pi, 500)\n",
"x, y = F(t)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trace = dict(\n",
" mode='markers', # line plot can't have a colorscale\n",
" x=x,\n",
" y=y,\n",
" marker=dict(\n",
" color=t,\n",
" showscale=True\n",
" \n",
" )\n",
")\n",
"\n",
"layout = dict(\n",
" autosize=False,\n",
" width=500,\n",
" height=500,\n",
" hovermode='closest'\n",
")\n",
"\n",
"fig = dict(data=[trace], layout=layout)\n",
"\n",
"py.iplot(fig, validate=False, filename='parametric-curve-3')\n",
"\n",
"# N.B. please enter validate=False as\n",
"# marker colorscale is not part of our validation system yet"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"\n",
"
\n",
"
\n",
"\n",
" Got Questions or Feedback?
\n",
"\n",
"About Plotly\n",
"\n",
"* email: feedback@plot.ly \n",
"* tweet: \n",
"@plotlygraphs\n",
"\n",
"Notebook styling ideas
\n",
"\n",
"Big thanks to\n",
"\n",
"* Cam Davidson-Pilon\n",
"* Lorena A. Barba\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, HTML\n",
"import urllib2\n",
"url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'\n",
"display(HTML(urllib2.urlopen(url).read()))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}