{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " require.config({\n", " baseUrl : \"https://cdn.rawgit.com/napjon/krisk/master/krisk/static\",\n", " paths: {\n", " echarts: \"https://cdnjs.cloudflare.com/ajax/libs/echarts/3.2.1/echarts.min\"\n", " }\n", " });\n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from ipywidgets import interact, IntSlider\n", "import ipywidgets as widgets\n", "\n", "import pandas as pd\n", "import krisk.plot as kk\n", "# Use this when you want to nbconvert the notebook (used by nbviewer)\n", "from krisk import init_notebook; init_notebook()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.read_csv('../krisk/tests/data/gapminderDataFiveYear.txt',sep='\\t')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Krisk also introduced a very simplistic for you to resync data or create a reproducible charts. Consider this plot," ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", "$('#e067b491-63a2-4efc-8f72-680f0f3ea7b3').attr('id','e067b491-63a2-4efc-8f72-680f0f3ea7b3'+'_old');\n", "element.append('
');\n", "require(['echarts', 'dark', 'vintage', 'roma', 'shine', 'infographic', 'macarons'],\n", "function(echarts){\n", " \n", " function parseFunction(str){\n", " return eval('(' + str + ')');\n", " }\n", " \n", " var myChart = echarts.init(document.getElementById(\"e067b491-63a2-4efc-8f72-680f0f3ea7b3\"),\"\");\n", " \n", " var option = {\n", " \"series\": [\n", " {\n", " \"data\": [\n", " 4570009.635,\n", " 13806097.84,\n", " 42283556.121,\n", " 13937361.533,\n", " 5343003.0\n", " ],\n", " \"name\": \"continent\",\n", " \"type\": \"bar\"\n", " }\n", " ],\n", " \"tooltip\": {\n", " \"axisPointer\": {\n", " \"type\": \"\"\n", " }\n", " },\n", " \"xAxis\": {\n", " \"data\": [\n", " \"Africa\",\n", " \"Americas\",\n", " \"Asia\",\n", " \"Europe\",\n", " \"Oceania\"\n", " ]\n", " },\n", " \"legend\": {\n", " \"data\": []\n", " },\n", " \"yAxis\": {},\n", " \"title\": {\n", " \"text\": \"\"\n", " }\n", "};\n", " option['tooltip']['formatter'] = parseFunction(option['tooltip']['formatter']);\n", " myChart.setOption(option);\n", " \n", " \n", " \n", "});\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = kk.bar(df[df.year == 1952],'continent',y='pop', how='mean')\n", "p.set_size(width=800)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Executing this code below " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", "require(['echarts', 'dark', 'vintage', 'roma', 'shine', 'infographic', 'macarons'],\n", "function(echarts){\n", " \n", " function parseFunction(str){\n", " return eval('(' + str + ')');\n", " }\n", " \n", " var myChart = echarts.init(document.getElementById(\"e067b491-63a2-4efc-8f72-680f0f3ea7b3\"),\"\");\n", " \n", " var option = {\n", " \"series\": [\n", " {\n", " \"data\": [\n", " 17875763.308,\n", " 35954847.36,\n", " 115513752.333,\n", " 19536617.633,\n", " 12274973.5\n", " ],\n", " \"name\": \"continent\",\n", " \"type\": \"bar\"\n", " }\n", " ],\n", " \"tooltip\": {\n", " \"axisPointer\": {\n", " \"type\": \"\"\n", " }\n", " },\n", " \"xAxis\": {\n", " \"data\": [\n", " \"Africa\",\n", " \"Americas\",\n", " \"Asia\",\n", " \"Europe\",\n", " \"Oceania\"\n", " ]\n", " },\n", " \"legend\": {\n", " \"data\": []\n", " },\n", " \"yAxis\": {},\n", " \"title\": {\n", " \"text\": \"\"\n", " }\n", "};\n", " option['tooltip']['formatter'] = parseFunction(option['tooltip']['formatter']);\n", " myChart.setOption(option);\n", " \n", " \n", " \n", "});\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.resync_data(df[df.year == 2007])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Would let you to modify the plot above instead of returning new plot. As you can see this is useful when you combine it to `ipywidgets` to interact with your data." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", "require(['echarts', 'dark', 'vintage', 'roma', 'shine', 'infographic', 'macarons'],\n", "function(echarts){\n", " \n", " function parseFunction(str){\n", " return eval('(' + str + ')');\n", " }\n", " \n", " var myChart = echarts.init(document.getElementById(\"e067b491-63a2-4efc-8f72-680f0f3ea7b3\"),\"\");\n", " \n", " var option = {\n", " \"series\": [\n", " {\n", " \"data\": [\n", " 4570009.635,\n", " 13806097.84,\n", " 42283556.121,\n", " 13937361.533,\n", " 5343003.0\n", " ],\n", " \"name\": \"continent\",\n", " \"type\": \"bar\"\n", " }\n", " ],\n", " \"tooltip\": {\n", " \"axisPointer\": {\n", " \"type\": \"\"\n", " }\n", " },\n", " \"xAxis\": {\n", " \"data\": [\n", " \"Africa\",\n", " \"Americas\",\n", " \"Asia\",\n", " \"Europe\",\n", " \"Oceania\"\n", " ]\n", " },\n", " \"legend\": {\n", " \"data\": []\n", " },\n", " \"yAxis\": {},\n", " \"title\": {\n", " \"text\": \"\"\n", " }\n", "};\n", " option['tooltip']['formatter'] = parseFunction(option['tooltip']['formatter']);\n", " myChart.setOption(option);\n", " \n", " \n", " \n", "});\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def resync(year):\n", " return p.resync_data(df[df.year == year])\n", "interact(resync,year=IntSlider(min=df.year.min(),max=df.year.max(),step=5,value=1952))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "You can also replot entirely the plot you have in the cell where the variable is" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", "require(['echarts', 'dark', 'vintage', 'roma', 'shine', 'infographic', 'macarons'],\n", "function(echarts){\n", " \n", " function parseFunction(str){\n", " return eval('(' + str + ')');\n", " }\n", " \n", " var myChart = echarts.init(document.getElementById(\"e067b491-63a2-4efc-8f72-680f0f3ea7b3\"),\"\");\n", " \n", " var option = {\n", " \"series\": [\n", " {\n", " \"data\": [\n", " 624,\n", " 396,\n", " 360,\n", " 300,\n", " 24\n", " ],\n", " \"name\": \"continent\",\n", " \"type\": \"line\"\n", " }\n", " ],\n", " \"tooltip\": {\n", " \"axisPointer\": {\n", " \"type\": \"\"\n", " }\n", " },\n", " \"xAxis\": {\n", " \"data\": [\n", " \"Africa\",\n", " \"Asia\",\n", " \"Europe\",\n", " \"Americas\",\n", " \"Oceania\"\n", " ]\n", " },\n", " \"legend\": {\n", " \"data\": []\n", " },\n", " \"yAxis\": {},\n", " \"title\": {\n", " \"text\": \"\"\n", " }\n", "};\n", " option['tooltip']['formatter'] = parseFunction(option['tooltip']['formatter']);\n", " myChart.setOption(option);\n", " \n", " \n", " \n", "});\n" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.replot(kk.line(df,'continent'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, there is `read_df` method for reproducible charts. You have a plot, and using it to just replace the data. This is especially useful when you have a beautiful chart already, and sharing to others just by replacing the data. Note that this is similar resync_data. But instead of replacing the cell where previous chart contained, it create new cell." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", "$('#11cd0150-0990-4a3b-a3ed-c1312a2e4bdc').attr('id','11cd0150-0990-4a3b-a3ed-c1312a2e4bdc'+'_old');\n", "element.append('
');\n", "require(['echarts', 'dark', 'vintage', 'roma', 'shine', 'infographic', 'macarons'],\n", "function(echarts){\n", " \n", " function parseFunction(str){\n", " return eval('(' + str + ')');\n", " }\n", " \n", " var myChart = echarts.init(document.getElementById(\"11cd0150-0990-4a3b-a3ed-c1312a2e4bdc\"),\"\");\n", " \n", " var option = {\n", " \"series\": [\n", " {\n", " \"data\": [\n", " 9916003.143,\n", " 24504794.997,\n", " 77038721.975,\n", " 17169764.733,\n", " 8874672.333\n", " ],\n", " \"name\": \"continent\",\n", " \"type\": \"bar\"\n", " }\n", " ],\n", " \"tooltip\": {\n", " \"axisPointer\": {\n", " \"type\": \"\"\n", " }\n", " },\n", " \"xAxis\": {\n", " \"data\": [\n", " \"Africa\",\n", " \"Americas\",\n", " \"Asia\",\n", " \"Europe\",\n", " \"Oceania\"\n", " ]\n", " },\n", " \"legend\": {\n", " \"data\": []\n", " },\n", " \"yAxis\": {},\n", " \"title\": {\n", " \"text\": \"\"\n", " }\n", "};\n", " option['tooltip']['formatter'] = parseFunction(option['tooltip']['formatter']);\n", " myChart.setOption(option);\n", " \n", " \n", " \n", "});\n" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.read_df(df)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }