Skip to content

Instantly share code, notes, and snippets.

@yngtodd
Created September 16, 2018 23:07
Show Gist options
  • Save yngtodd/5a22efddac25d958afdcdb01ec12a267 to your computer and use it in GitHub Desktop.
Save yngtodd/5a22efddac25d958afdcdb01ec12a267 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}</script>"
],
"text/vnd.plotly.v1+html": [
"<script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\"\"\"\n",
"Starter pack for plotting with Plotly.\n",
"\n",
"Reference:\n",
"---------\n",
"https://www.youtube.com/watch?v=XUNaGFa9xCM\n",
"\"\"\"\n",
"\n",
"import plotly\n",
"import plotly.graph_objs as go\n",
"import ipywidgets as widgets\n",
"\n",
"import numpy as np\n",
"from scipy import special\n",
"\n",
"plotly.offline.init_notebook_mode(connected=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace(0, 2*np.pi, 1000)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1388863534664d85b4f8c6931c926ee8",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(SelectMultiple(description='Bessel Order', index=(0,), options=(0, 1, 2, 3, 4, 5), value…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"layout = go.Layout(\n",
" title = 'Simple Example',\n",
" yaxis = dict(\n",
" title = 'volts'\n",
" ),\n",
" xaxis = dict(\n",
" title = 'nanoseconds'\n",
" )\n",
")\n",
"\n",
"def update_plot(signals, freq):\n",
" data = []\n",
" for s in signals:\n",
" trace = go.Scatter(\n",
" x = x,\n",
" y = special.jv(s, freq*x),\n",
" mode = 'lines',\n",
" name = f'bessel {s}',\n",
" line = dict(\n",
" shape = 'spline'\n",
" )\n",
" )\n",
" data.append(trace)\n",
"\n",
" fig = go.Figure(data=data, layout=layout)\n",
" plotly.offline.iplot(fig)\n",
" \n",
"signals = widgets.SelectMultiple(options=list(range(6)), value=(0,), description='Bessel Order')\n",
"freq = widgets.FloatSlider(min=1, max=20, value=1, description='Freq')\n",
"widgets.interactive(update_plot, signals=signals, freq=freq)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"data": [
{
"type": "contour",
"uid": "751b39c2-ba01-11e8-b089-b8e856187c76",
"z": [
[
10,
10.625,
12.5,
15.625,
20
],
[
5.625,
6.25,
8.125,
11.25,
15.625
]
]
}
],
"layout": {}
},
"text/html": [
"<div id=\"47a350a1-ee84-43fe-b829-8e1a59ddb707\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"47a350a1-ee84-43fe-b829-8e1a59ddb707\", [{\"z\": [[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625]], \"type\": \"contour\", \"uid\": \"752100be-ba01-11e8-b20d-b8e856187c76\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\"})});</script>"
],
"text/vnd.plotly.v1+html": [
"<div id=\"47a350a1-ee84-43fe-b829-8e1a59ddb707\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"47a350a1-ee84-43fe-b829-8e1a59ddb707\", [{\"z\": [[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625]], \"type\": \"contour\", \"uid\": \"752100be-ba01-11e8-b20d-b8e856187c76\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\"})});</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625]]\n",
" )\n",
"]\n",
"plotly.offline.iplot(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"def styblinski(x, y):\n",
" return(1.0/2 * (x**4 - 16*x**2 + 5*x + y**4 - 16*y**2 + 5*y))\n",
"\n",
"xx = np.linspace(-5, 5, 200)\n",
"yy = xx\n",
"x,y = np.meshgrid(x, y)\n",
"z = styblinski(x, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"colorscale=[[0.0, 'rgb(20,29,67)'],\n",
" [0.1, 'rgb(28,76,96)'],\n",
" [0.2, 'rgb(16,125,121)'],\n",
" [0.3, 'rgb(92,166,133)'],\n",
" [0.4, 'rgb(182,202,175)'],\n",
" [0.5, 'rgb(253,245,243)'],\n",
" [0.6, 'rgb(230,183,162)'],\n",
" [0.7, 'rgb(211,118,105)'],\n",
" [0.8, 'rgb(174,63,95)'],\n",
" [0.9, 'rgb(116,25,93)'],\n",
" [1.0, 'rgb(51,13,53)']]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"textz=[['x: '+'{:0.5f}'.format(x[i][j])+'<br>y: '+'{:0.5f}'.format(y[i][j])+\n",
" '<br>z: '+'{:0.5f}'.format(z[i][j]) for j in range(z.shape[1])] for i in range(z.shape[0])]\n",
"\n",
"trace1= go.Surface(z=z,\n",
" x=x,\n",
" y=y,\n",
" colorscale=colorscale,\n",
" text=textz,\n",
" hoverinfo='text',\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"axis = dict(\n",
"showbackground=True,\n",
"backgroundcolor=\"rgb(230, 230,230)\",\n",
"showgrid=False,\n",
"zeroline=False,\n",
"showline=False)\n",
"\n",
"ztickvals=list(range(-6,4))\n",
"layout = go.Layout(title=\"Projections of a surface onto coordinate planes\" ,\n",
" autosize=False,\n",
" width=700,\n",
" height=600,\n",
" scene=dict(xaxis=dict(axis, range=[-3.5, 3.5]),\n",
" yaxis=dict(axis, range=[-3.5, 3.5]),\n",
" zaxis=dict(axis , tickvals=ztickvals),\n",
" aspectratio=dict(x=1,\n",
" y=1,\n",
" z=0.95)\n",
" )\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z_offset=(np.min(z)-2)*np.ones(z.shape)#\n",
"x_offset=np.min(xx)*np.ones(z.shape)\n",
"y_offset=np.min(yy)*np.ones(z.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"proj_z=lambda x, y, z: z#projection in the z-direction\n",
"colorsurfz=proj_z(x,y,z)\n",
"proj_x=lambda x, y, z: x\n",
"colorsurfx=proj_z(x,y,z)\n",
"proj_y=lambda x, y, z: y\n",
"colorsurfy=proj_z(x,y,z)\n",
"\n",
"textx=[['y: '+'{:0.5f}'.format(y[i][j])+'<br>z: '+'{:0.5f}'.format(z[i][j])+\n",
" '<br>x: '+'{:0.5f}'.format(x[i][j]) for j in range(z.shape[1])] for i in range(z.shape[0])]\n",
"texty=[['x: '+'{:0.5f}'.format(x[i][j])+'<br>z: '+'{:0.5f}'.format(z[i][j]) +\n",
" '<br>y: '+'{:0.5f}'.format(y[i][j]) for j in range(z.shape[1])] for i in range(z.shape[0])]\n",
"\n",
"tracex = go.Surface(z=z,\n",
" x=x_offset,\n",
" y=y,\n",
" colorscale=colorscale,\n",
" showlegend=False,\n",
" showscale=False,\n",
" surfacecolor=colorsurfx,\n",
" text=textx,\n",
" hoverinfo='text'\n",
" )\n",
"tracey = go.Surface(z=z,\n",
" x=x,\n",
" y=y_offset,\n",
" colorscale=colorscale,\n",
" showlegend=False,\n",
" showscale=False,\n",
" surfacecolor=colorsurfy,\n",
" text=texty,\n",
" hoverinfo='text'\n",
" )\n",
"tracez = go.Surface(z=z_offset,\n",
" x=x,\n",
" y=y,\n",
" colorscale=colorscale,\n",
" showlegend=False,\n",
" showscale=False,\n",
" surfacecolor=colorsurfx,\n",
" text=textz,\n",
" hoverinfo='text'\n",
" )\n",
"\n",
"data=[trace1, tracex, tracey, tracez]\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment