Created
February 11, 2014 17:58
-
-
Save anonymous/8940322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Running [MATLAB in an IPython Notebook](https://github.com/arokem/python-matlab-bridge)\n", | |
"=====================================\n", | |
"\n", | |
"With [Interactive MATLAB Graphing in Plotly](https://plot.ly/api/MATLAB)\n", | |
"------------------------------------------------------------------------\n", | |
"\n", | |
"Check out more examples interactive Plotly graphs in IPython notebooks here: [nbviewer.ipython.org/github/plotly/IPython-plotly/tree/master/](nbviewer.ipython.org/github/plotly/IPython-plotly/tree/master/)\n", | |
"\n", | |
"Questions? <andrey@plot.ly>, [@plotlygraphs](http://twitter.com/plotlygraphs)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The following is a list of usage examples of Plotly in Matlab. The original example data can be found at the [Matlab Plot Gallery](http://www.mathworks.com/discovery/gallery.html)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"First, we need to start a MATLAB server:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import pymatbridge as pymat\n", | |
"ip = get_ipython()\n", | |
"pymat.load_ipython_extension(ip, \n", | |
" matlab='C:\\Users\\Andrey\\matlab_shortcut.lnk')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Starting MATLAB on http://localhost:55167\n", | |
" visit http://localhost:55167/exit.m to shut down same\n", | |
"." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"MATLAB started and connected!\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Note to Windows users: Python doesn't like file paths with spaces! Above, I created a shortcut to matlab.exe and placed it in path that did not contain spaces. It's a bit of a hack, there are better ways to do it I'm sure." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Now we are ready to run MATLAB! The following code segments follow the same genearal structure: connect to Plotly, generate the necessary data structures, and send the graph request. The response is the url where the plot is located." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"EXAMPLE 1: Morse Signal Analysis" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%matlab\n", | |
"% set up plotly environment\n", | |
"clear all\n", | |
"\n", | |
"api_path = 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\API_matlab\\plotly';\n", | |
"addpath(genpath(api_path))\n", | |
"signin('andreyDim', 'r0x3w3ivg4');\n", | |
"\n", | |
"% Load Morse data\n", | |
"load 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\Matlab_Examples\\Line_Plot_2D_3\\MDdata'\n", | |
"\n", | |
"% specify the style of each trace\n", | |
"data1 = struct(...\n", | |
" 'x', dissimilarities, ...\n", | |
" 'y', dist1, ...\n", | |
" 'type', 'scatter', ...\n", | |
" 'name', 'Stress', ...\n", | |
" 'mode', 'markers', ...\n", | |
" 'marker', struct('opacity', 0.5, ...\n", | |
" 'symbol','triangle-up', ... \n", | |
" 'size',8, ...\n", | |
" 'color', 'rgb(54,144,192)' ...\n", | |
" ) ...\n", | |
" );\n", | |
"\n", | |
"data2=data1;\n", | |
"data2.y = dist2;\n", | |
"data2.name = 'Sammon Mapping';\n", | |
"data2.marker.symbol = 'cross';\n", | |
"data2.marker.color = 'rgb(144,192,54)';\n", | |
"\n", | |
"data3=data1;\n", | |
"data3.y = dist3;\n", | |
"data3.name = 'Squared Stress';\n", | |
"data3.marker.symbol = 'square';\n", | |
"data3.marker.color = 'rgb(192,54,144)';\n", | |
"\n", | |
"% set up axis styles\n", | |
"axesstyle_x = struct( ...\n", | |
" 'zeroline' , false, ...\n", | |
" 'title' , 'Dissimilarities' ...\n", | |
");\n", | |
"axesstyle_y = struct( ...\n", | |
" 'zeroline' , false, ...\n", | |
" 'title' , 'Distances' ...\n", | |
");\n", | |
"\n", | |
"% set the legend style\n", | |
"legendstyle = struct('x' , 0, 'y' , 1); % Top left\n", | |
"\n", | |
"% set up layout\n", | |
"layout = struct( ...\n", | |
" 'title', 'Morse Signal Analysis', ...\n", | |
" 'xaxis' , axesstyle_x, ...\n", | |
" 'yaxis' , axesstyle_y, ...\n", | |
" 'legend' , legendstyle, ...\n", | |
" 'hovermode', 'closest' ...\n", | |
");\n", | |
" \n", | |
"% send graph request\n", | |
"response = plotly({data1, data2, data3}, struct('layout', layout, ...\n", | |
" 'filename', 'morse_signal','fileopt', 'overwrite'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "display_data", | |
"text": [ | |
"\r\n", | |
"response = \r\n", | |
"\r\n", | |
" url: 'https://plot.ly/~andreyDim/142'\r\n", | |
" message: [1x0 char]\r\n", | |
" warning: [1x0 char]\r\n", | |
" filename: 'morse_signal'\r\n", | |
" error: [1x0 char]\r\n", | |
"\r\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The `plotly` MATLAB command sends data to my [plotly account](https://plot.ly/~andreyDim) and returns the unique url [https://plot.ly/~andreyDim/142](https://plot.ly/~andreyDim/142) where anyone can view the rendered graph.\n", | |
"\n", | |
"Every plotly graph URL can be [embedded in HTML as wrapped in an IFrame](https://plot.ly/api/MATLAB/docs/iframes). Here's a quick utility function that will embed a plotly graph in IPython notebooks." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from IPython.display import HTML\n", | |
"\n", | |
"def show_plot(url, width=700, height=500):\n", | |
" s = '<iframe height=\"%s\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"%s\" width=\"%s\"></iframe>' %\\\n", | |
" (height+50, \"/\".join(map(str,[url, width, height])), width+50)\n", | |
" return HTML(s)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let's see the the graph:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"show_plot('https://plot.ly/~andreyDim/142')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<iframe height=\"550\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~andreyDim/142/700/500\" width=\"750\"></iframe>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"<IPython.core.display.HTML at 0x5701a58>" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Sweet! Let's keep going..." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"EXAMPLE 2: Gain vs Frequency" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%matlab\n", | |
"% Prepare plotly environment\n", | |
"clear all\n", | |
"api_path = 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\API_matlab\\plotly';\n", | |
"addpath(genpath(api_path))\n", | |
"signin('andreyDim', 'r0x3w3ivg4');\n", | |
"\n", | |
"% Create a set of values for the damping factor\n", | |
"zeta = [0.01 .02 0.05 0.1 .2 .5 1 ];\n", | |
"\n", | |
"% Create a range of frequency values equally spaced logarithmically\n", | |
"w = logspace(-1, 1, 1000);\n", | |
"\n", | |
"% Define a color for each damping factor\n", | |
"colors = {'red' 'green' 'blue' 'lightblue' 'purple' 'lightgreen' 'fuchsia'};\n", | |
"\n", | |
"% Create PLOTLY data structs\n", | |
"data = cell(1,7);\n", | |
"\n", | |
"% Produce gain vs. frequency for each of the seven damping factors\n", | |
"for i = 1:7\n", | |
" a = w.^2 - 1;\n", | |
" b = 2*w*zeta(i);\n", | |
" gain = sqrt(1./(a.^2 + b.^2));\n", | |
" \n", | |
" \n", | |
" data{i} = struct(...\n", | |
" 'x', w, ...\n", | |
" 'y', gain, ...\n", | |
" 'type', 'scatter', ...\n", | |
" 'name', ['$\\\\zeta = ' num2str(zeta(i)) '$'], ...\n", | |
" 'mode', 'lines', ...\n", | |
" 'line', struct('color',colors{i},...\n", | |
" 'width',3) ...\n", | |
" );\n", | |
" \n", | |
"end\n", | |
"\n", | |
"% set axis styles\n", | |
"axesstyle_x = struct( ...\n", | |
" 'type' , 'log', ...\n", | |
" 'range' , [-1, 1], ...\n", | |
" 'linewidth' , 2, ...\n", | |
" 'title' , '$\\\\text{Frequency } \\\\omega$' ...\n", | |
");\n", | |
"axesstyle_y = struct( ...\n", | |
" 'type' , 'log', ...\n", | |
" 'range' , [-2, 2], ...\n", | |
" 'linewidth' , 2, ...\n", | |
" 'title' , '$\\\\text{Gain}$' ...\n", | |
");\n", | |
"\n", | |
"% set up legend style\n", | |
"legendstyle = struct( ...\n", | |
" 'x' , 0, ...\n", | |
" 'y' , 1, ...\n", | |
" 'bgcolor' , '#E2E2E2', ... \n", | |
" 'bordercolor' , '#FFFFFF', ...\n", | |
" 'borderwidth' , 2, ...\n", | |
" 'traceorder' , 'normal' ...\n", | |
" );\n", | |
"\n", | |
"% set layout style\n", | |
"layout = struct( ...\n", | |
" 'title', '$\\\\text{Gain vs Frequency } \\\\omega$', ...\n", | |
" 'xaxis' , axesstyle_x, ...\n", | |
" 'yaxis' , axesstyle_y, ...\n", | |
" 'legend', legendstyle, ...\n", | |
" 'hovermode', 'closest' ...\n", | |
");\n", | |
"\n", | |
"% send graph request\n", | |
"response = plotly(data, struct('layout', layout, ...\n", | |
" 'filename','gain_frequency', ...\n", | |
"\t'fileopt', 'overwrite'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "display_data", | |
"text": [ | |
"\r\n", | |
"response = \r\n", | |
"\r\n", | |
" url: 'https://plot.ly/~andreyDim/139'\r\n", | |
" message: [1x0 char]\r\n", | |
" warning: [1x0 char]\r\n", | |
" filename: 'gain_frequency'\r\n", | |
" error: [1x0 char]\r\n", | |
"\r\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"show_plot('https://plot.ly/~andreyDim/139')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<iframe height=\"550\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~andreyDim/139/700/500\" width=\"750\"></iframe>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": [ | |
"<IPython.core.display.HTML at 0x570f0b8>" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"EXAMPLE 3: Tuberculosis Cases" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%matlab\n", | |
"\n", | |
"% Prepare plotly environment\n", | |
"clear all\n", | |
"api_path = 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\API_matlab\\plotly';\n", | |
"addpath(genpath(api_path))\n", | |
"signin('andreyDim', 'r0x3w3ivg4');\n", | |
"\n", | |
"% Create the data for the plots\n", | |
"TBdata = [1990 4889 16.4; 1991 5273 17.4; 1992 5382 17.4; 1993 5173 16.5;\n", | |
" 1994 4860 15.4; 1995 4675 14.7; 1996 4313 13.5; 1997 4059 12.5;\n", | |
" 1998 3855 11.7; 1999 3608 10.8; 2000 3297 9.7; 2001 3332 9.6;\n", | |
" 2002 3169 9.0; 2003 3227 9.0; 2004 2989 8.2; 2005 2903 7.9;\n", | |
" 2006 2779 7.4; 2007 2725 7.2];\n", | |
"\n", | |
"years = TBdata(:,1);\n", | |
"cases = TBdata(:,2);\n", | |
"rate = TBdata(:,3);\n", | |
"\n", | |
"% specify the style of each trace\n", | |
"data1 = struct('name', 'Cases', ...\n", | |
" 'opacity', 1.0, ...\n", | |
" 'x', TBdata(:,1), ...\n", | |
" 'y', TBdata(:,2), ...\n", | |
" 'xaxis','x', ...\n", | |
" 'yaxis','y', ...\n", | |
" 'type', 'bar', ...\n", | |
" 'marker', struct(...\n", | |
" 'color', 'white') ...\n", | |
");\n", | |
"\n", | |
"data2 = struct('name', 'Infection rate in cases per thousand', ...\n", | |
" 'opacity', 1.0, ...\n", | |
" 'x', TBdata(:,1), ...\n", | |
" 'y', TBdata(:,3), ...\n", | |
" 'xaxis','x', ...\n", | |
" 'yaxis','y2', ...\n", | |
" 'type', 'scatter', ...\n", | |
" 'mode', 'lines', ...\n", | |
" 'line', struct('color', 'purple', 'size', 2) ...\n", | |
" );\n", | |
"\n", | |
"% set the axis styles\n", | |
"axesstyle_x = struct( ...\n", | |
" 'title' , 'Years' ...\n", | |
");\n", | |
"axesstyle_y = struct( ...\n", | |
" 'range', [0 6000], ...\n", | |
" 'ticks' , '', ...\n", | |
" 'title' , 'Cases' ...\n", | |
");\n", | |
"axesstyle_y2 = struct( ...\n", | |
" 'showgrid' , false, ...\n", | |
" 'ticks' , '', ...\n", | |
" 'range', [6 18], ...\n", | |
" 'titlefont',struct( ...\n", | |
" 'color','purple' ...\n", | |
" ), ...\n", | |
" 'tickfont',struct( ...\n", | |
" 'color','purple' ...\n", | |
" ), ...\n", | |
" 'overlaying','y', ...\n", | |
" 'side','right', ...\n", | |
" 'title' , 'Infection rate in cases per thousand' ...\n", | |
");\n", | |
"\n", | |
"% set the layout style\n", | |
"layout = struct( ...\n", | |
" 'title', 'Tuberculosis Cases: 1991-2007', ...\n", | |
" 'xaxis' , axesstyle_x, ...\n", | |
" 'yaxis' , axesstyle_y, ...\n", | |
" 'yaxis2', axesstyle_y2, ...\n", | |
" 'plot_bgcolor', '#c7c7c7', ...\n", | |
" 'showlegend', false ...\n", | |
");\n", | |
" \n", | |
"% send graph request\n", | |
"response = plotly({data1, data2}, struct('layout', layout, ...\n", | |
" 'filename','Tuberculosis_cases', ...\n", | |
"\t'fileopt', 'overwrite'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "display_data", | |
"text": [ | |
"\r\n", | |
"response = \r\n", | |
"\r\n", | |
" url: 'https://plot.ly/~andreyDim/143'\r\n", | |
" message: [1x0 char]\r\n", | |
" warning: [1x0 char]\r\n", | |
" filename: 'Tuberculosis_cases'\r\n", | |
" error: [1x0 char]\r\n", | |
"\r\n" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"show_plot('https://plot.ly/~andreyDim/143')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<iframe height=\"550\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~andreyDim/143/700/500\" width=\"750\"></iframe>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": [ | |
"<IPython.core.display.HTML at 0x570f080>" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"EXAMPLE 4: Curve Fit" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%matlab\n", | |
"%set up plotly environment\n", | |
"clear all\n", | |
"api_path = 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\API_matlab\\plotly';\n", | |
"addpath(genpath(api_path))\n", | |
"signin('andreyDim', 'r0x3w3ivg4');\n", | |
"\n", | |
"% Load the data for x, y, and yfit\n", | |
"load 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\Matlab_Examples\\Curve_Fit_with_Confidence_Bounds\\fitdata'\n", | |
"\n", | |
"% set up scatter of samples\n", | |
"data1 = struct('name', 'Samples', ...\n", | |
" 'opacity', 1.0, ...\n", | |
" 'x', x, ...\n", | |
" 'y', y, ...\n", | |
" 'type', 'scatter', ...\n", | |
" 'mode', 'markers', ...\n", | |
" 'marker', struct('color', 'black') ...\n", | |
" );\n", | |
"\n", | |
"% set up curve fit\n", | |
"data2=data1;\n", | |
"data2.y = yfit;\n", | |
"data2.name = 'Localized Regression';\n", | |
"data2.mode = 'lines';\n", | |
"data2.line = struct('color', 'red', 'width', 4);\n", | |
"\n", | |
"% set up confidence intervals\n", | |
"data3 = data2;\n", | |
"data3.y = yfit+0.3;\n", | |
"data3.name = 'Confidence Interval';\n", | |
"data3.line = struct('color', 'purple', 'width', 1, 'dash', 'dashdot');\n", | |
"\n", | |
"data4 = data3;\n", | |
"data4.fill = 'tonexty';\n", | |
"data4.y = yfit-0.3;\n", | |
"\n", | |
"% set the axis style\n", | |
"axesstyle = struct( ...\n", | |
" 'zeroline' , false, ...\n", | |
" 'ticks' , '', ...\n", | |
" 'linecolor' , '#636363', ...\n", | |
" 'linewidth' , 2 ...\n", | |
");\n", | |
"\n", | |
"% set the layout style\n", | |
"layout = struct( ...\n", | |
" 'title', 'Curve Fit Visualization', ...\n", | |
" 'xaxis' , axesstyle, ...\n", | |
" 'yaxis' , axesstyle, ...\n", | |
" 'hovermode', 'closest', ...\n", | |
" 'showlegend', false ...\n", | |
");\n", | |
"\n", | |
"% send graph request\n", | |
"response = plotly({data1, data2, data3, data4}, struct('layout', layout, ...\n", | |
" 'filename','curve_fit_confidence', ...\n", | |
"\t'fileopt', 'overwrite'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "display_data", | |
"text": [ | |
"\r\n", | |
"response = \r\n", | |
"\r\n", | |
" url: 'https://plot.ly/~andreyDim/138'\r\n", | |
" message: [1x0 char]\r\n", | |
" warning: [1x0 char]\r\n", | |
" filename: 'curve_fit_confidence'\r\n", | |
" error: [1x0 char]\r\n", | |
"\r\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"show_plot('https://plot.ly/~andreyDim/138')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<iframe height=\"550\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~andreyDim/138/700/500\" width=\"750\"></iframe>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 13, | |
"text": [ | |
"<IPython.core.display.HTML at 0x570f668>" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"EXAMPLE 5: Spline and Convex Hull Property" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%matlab\n", | |
"%set up plotly environment\n", | |
"clear all\n", | |
"api_path = 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\API_matlab\\plotly';\n", | |
"addpath(genpath(api_path))\n", | |
"signin('andreyDim', 'r0x3w3ivg4');\n", | |
"\n", | |
"% Load the points for creating a spline curve\n", | |
"load 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\Matlab_Examples\\Add_Text_to_Plot_1\\splineData'\n", | |
"\n", | |
"% set up text\n", | |
"text = cell(1,size(points,1));\n", | |
"for i=1:numel(text)\n", | |
" text{i} = ['Point ' num2str(i)]; \n", | |
"end\n", | |
"\n", | |
"% set up scatter data\n", | |
"data1 = struct( ...\n", | |
" 'name', 'Control Points', ...\n", | |
" 'x', points(:,1), ...\n", | |
" 'y', points(:,2), ...\n", | |
" 'marker', struct( ...\n", | |
" 'color', 'red'), ...\n", | |
" 'mode', 'lines+markers+text',...\n", | |
" 'line', struct('color', 'black', 'width', 1, 'dash', 'dot'), ...\n", | |
" 'text', {text}, ...\n", | |
" 'textposition', 'top'...\n", | |
" );\n", | |
"\n", | |
"% set up spline\n", | |
"data2 = struct( ...\n", | |
" 'name', 'Spline', ...\n", | |
" 'x', x, ...\n", | |
" 'y', y, ...\n", | |
" 'mode', 'lines',...\n", | |
" 'line', struct('color', 'red', 'width', 3) ...\n", | |
" );\n", | |
"\n", | |
"\n", | |
"% set the axis style\n", | |
"axesstyle = struct( ...\n", | |
" 'showgrid' , false, ...\n", | |
" 'zeroline' , false, ...\n", | |
" 'showticklabels' , false, ...\n", | |
" 'ticks' , '', ...\n", | |
" 'linecolor' , '#636363', ...\n", | |
" 'linewidth' , 3 ...\n", | |
");\n", | |
"\n", | |
"% set the layout style\n", | |
"layout = struct( ...\n", | |
" 'title', 'The Convex-Hull Property', ...\n", | |
" 'xaxis' , axesstyle, ...\n", | |
" 'yaxis' , axesstyle, ...\n", | |
" 'hovermode', 'closest', ...\n", | |
" 'showlegend', false ...\n", | |
");\n", | |
" \n", | |
"% send graph request\n", | |
"response = plotly({data1, data2}, struct('layout', layout, ...\n", | |
" 'filename', 'convex_hull', 'fileopt','overwrite'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "display_data", | |
"text": [ | |
"\r\n", | |
"response = \r\n", | |
"\r\n", | |
" url: 'https://plot.ly/~andreyDim/141'\r\n", | |
" message: [1x0 char]\r\n", | |
" warning: [1x0 char]\r\n", | |
" filename: 'convex_hull'\r\n", | |
" error: [1x0 char]\r\n", | |
"\r\n" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"show_plot('https://plot.ly/~andreyDim/141')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<iframe height=\"550\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~andreyDim/141/700/500\" width=\"750\"></iframe>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": [ | |
"<IPython.core.display.HTML at 0x570f940>" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"EXAMPLE 6: Undersea Elevation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%matlab\n", | |
"% set up plotly environment\n", | |
"clear all\n", | |
"\n", | |
"api_path = 'C:\\Users\\Andrey\\Dropbox\\ANDREY\\PLOTLY\\API_matlab\\plotly';\n", | |
"addpath(genpath(api_path))\n", | |
"signin('andreyDim', 'r0x3w3ivg4');\n", | |
"\n", | |
"% Load undersea elevation data\n", | |
"load seamount x y z;\n", | |
"\n", | |
"% set up color map for z values\n", | |
"step_size = (max(z)-min(z))/99;\n", | |
"map_idx = ceil((z - min(z))/step_size)+1;\n", | |
"color_map = jet(100);\n", | |
"\n", | |
"colors = cell(1,numel(x));\n", | |
"for i=1:numel(x)\n", | |
"colors{i} = ['rgb(' num2str(floor(255*color_map(map_idx(i),1))) ',' ...\n", | |
" num2str(floor(255*color_map(map_idx(i),2))) ',' ...\n", | |
" num2str(floor(255*color_map(map_idx(i),3))) ')'];\n", | |
"end\n", | |
"\n", | |
"% set up scatter data\n", | |
"data = struct( ...\n", | |
" 'x', x, ...\n", | |
" 'y', y, ...\n", | |
" 'marker', struct( ...\n", | |
" 'color', {colors}), ...\n", | |
" 'mode', 'markers' ...\n", | |
" );\n", | |
"\n", | |
"\n", | |
"\n", | |
"% set the axis styles\n", | |
"axesstyle_x = struct( ...\n", | |
" 'title' , 'Longitude' ...\n", | |
");\n", | |
"axesstyle_y = struct( ...\n", | |
" 'title' , 'Latitude' ...\n", | |
");\n", | |
"\n", | |
"\n", | |
"% set the layout style\n", | |
"layout = struct( ...\n", | |
" 'title', 'Undersea Elevation', ...\n", | |
" 'xaxis' , axesstyle_x, ...\n", | |
" 'yaxis' , axesstyle_y, ...\n", | |
" 'hovermode', 'closest', ...\n", | |
" 'showlegend', false ...\n", | |
");\n", | |
" \n", | |
"% send graph request\n", | |
"response = plotly(data, struct('layout', layout, ...\n", | |
" 'filename', 'undersea_elevation','fileopt', 'overwrite'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "display_data", | |
"text": [ | |
"\r\n", | |
"response = \r\n", | |
"\r\n", | |
" url: 'https://plot.ly/~andreyDim/140'\r\n", | |
" message: [1x0 char]\r\n", | |
" warning: [1x0 char]\r\n", | |
" filename: 'undersea_elevation'\r\n", | |
" error: [1x0 char]\r\n", | |
"\r\n" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"show_plot('https://plot.ly/~andreyDim/140')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<iframe height=\"550\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~andreyDim/140/700/500\" width=\"750\"></iframe>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 17, | |
"text": [ | |
"<IPython.core.display.HTML at 0x570f710>" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"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