Created
November 2, 2016 14:21
-
-
Save bjackman/559553d565b033ad3cd7799bf9f9c0de 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Poor-man's stacked plot using fill_alpha" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from math import sin\n", | |
"\n", | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"\n", | |
"import trappy" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Create some data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"n = 10\n", | |
"cubes = [x ** 3 for x in range(n)]\n", | |
"squares = [x ** 2 for x in range(n)]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Use DataFrame::apply(np.cumsum, axis=1) to pre-process DataFrame" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"df = pd.DataFrame({\"cubes\": cubes, \"squares\": squares}, index=range(10)).apply(np.cumsum, axis=1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Use fill_alpha=1.0 to show the sum of `cubes` and `squares` and contribution of each" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table style=\"border-style: hidden;\">\n", | |
"<tr>\n", | |
"<td style=\"border-style: hidden;\"><div class=\"ilineplot\" id=\"fig_b86c656547cb4ae6916315c6d81ee774\"></div></td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
"<td style=\"border-style: hidden;\"><div style=\"text-align:center\" id=\"fig_b86c656547cb4ae6916315c6d81ee774_legend\"></div></td>\n", | |
"</tr>\n", | |
"</table>\n", | |
"<script>\n", | |
"var fig_b86c656547cb4ae6916315c6d81ee774_data = {\"logscale\": false, \"strokeWidth\": 1.0, \"pointSize\": 2, \"name\": \"fig_b86c656547cb4ae6916315c6d81ee774\", \"step_plot\": true, \"fill_graph\": true, \"title\": \"\", \"fill_alpha\": 1.0, \"drawPoints\": false, \"per_line\": 1, \"height\": 400, \"rangesel\": false, \"data\": {\"labels\": [\"index\", \"DataFrame 0:cubes\", \"DataFrame 0:squares\"], \"data\": [[0, 0, 0], [1, 1, 2], [2, 8, 12], [3, 27, 36], [4, 64, 80], [5, 125, 150], [6, 216, 252], [7, 343, 392], [8, 512, 576], [9, 729, 810]]}};\n", | |
"</script>\n", | |
"<!-- TRAPPY_PUBLISH_SOURCE_LIB = \"http://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js\" -->\n", | |
"<!-- TRAPPY_PUBLISH_SOURCE_LIB = \"http://dygraphs.com/extras/synchronizer.js\" -->\n", | |
"<!-- TRAPPY_PUBLISH_SOURCE_LIB = \"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js\" -->\n", | |
"\n", | |
" <script>\n", | |
" /* TRAPPY_PUBLISH_IMPORT = \"plotter/js/ILinePlot.js\" */\n", | |
" /* TRAPPY_PUBLISH_REMOVE_START */\n", | |
" var ilp_req = require.config( {\n", | |
"\n", | |
" paths: {\n", | |
" \"dygraph-sync\": '/nbextensions/plotter_scripts/ILinePlot/synchronizer',\n", | |
" \"dygraph\": '/nbextensions/plotter_scripts/ILinePlot/dygraph-combined',\n", | |
" \"ILinePlot\": '/nbextensions/plotter_scripts/ILinePlot/ILinePlot',\n", | |
" \"underscore\": '/nbextensions/plotter_scripts/ILinePlot/underscore-min',\n", | |
" },\n", | |
"\n", | |
" shim: {\n", | |
" \"dygraph-sync\": [\"dygraph\"],\n", | |
" \"ILinePlot\": {\n", | |
"\n", | |
" \"deps\": [\"dygraph-sync\", \"dygraph\", \"underscore\"],\n", | |
" \"exports\": \"ILinePlot\"\n", | |
" }\n", | |
" }\n", | |
" });\n", | |
" /* TRAPPY_PUBLISH_REMOVE_STOP */\n", | |
" ilp_req([\"require\", \"ILinePlot\"], function() { /* TRAPPY_PUBLISH_REMOVE_LINE */\n", | |
" ILinePlot.generate(fig_b86c656547cb4ae6916315c6d81ee774_data);\n", | |
" }); /* TRAPPY_PUBLISH_REMOVE_LINE */\n", | |
" </script>\n", | |
" " | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"trappy.ILinePlot(df, column=df.columns.tolist(), drawstyle=\"steps-post\", fill_alpha=1.0).view()" | |
] | |
} | |
], | |
"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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By the way, the JavaScript doesn't work when it's hosted on GitHub so you'll need to download this and view it from a local notebook server.