Created
May 22, 2017 17:08
-
-
Save stoneyv/df80e7cdfcd64ad6199c6faccccd215d to your computer and use it in GitHub Desktop.
python plotnine geom_polygon test adapted from ggplot2 documentation
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": [ | |
"## plotnine geom_polygon test\n", | |
"https://plotnine.readthedocs.io/en/stable/generated/plotnine.geoms.geom_polygon.html#plotnine.geoms.geom_polygon" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"python version 3.6.0\n", | |
"numpy version 1.11.3\n", | |
"pandas version 0.20.1\n" | |
] | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"import sys\n", | |
"\n", | |
"version = \".\".join(map(str, sys.version_info[:3]))\n", | |
"print('python version ', version)\n", | |
"print('numpy version ', np.__version__)\n", | |
"print('pandas version ',pd.__version__)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/home/stoney/anaconda3/lib/python3.6/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n", | |
" from pandas.core import datetools\n" | |
] | |
} | |
], | |
"source": [ | |
"from plotnine import *\n", | |
"from plotnine.layer import Layers\n", | |
"\n", | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## geom_polygon example adapted from ggplot2 documentation\n", | |
"http://ggplot2.tidyverse.org/reference/geom_polygon.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ids = ['1.1','2.1','1.2','2.2','1.3','2.3']\n", | |
"\n", | |
"values = { 'iid': ids,\n", | |
" 'value':(3, 3.1, 3.1, 3.2, 3.15, 3.5)}\n", | |
"\n", | |
"positions = { 'iid': ids*4,\n", | |
" 'x':(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,\n", | |
" 0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),\n", | |
" 'y':(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,\n", | |
" 2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)}\n", | |
"\n", | |
"positions_df = pd.DataFrame(positions)\n", | |
"values_df = pd.DataFrame(values)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"datapoly_df shape as rows,columns (24, 4)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style>\n", | |
" .dataframe thead tr:only-child th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: left;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>iid</th>\n", | |
" <th>x</th>\n", | |
" <th>y</th>\n", | |
" <th>value</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1.1</td>\n", | |
" <td>2.0</td>\n", | |
" <td>-0.5</td>\n", | |
" <td>3.00</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2.1</td>\n", | |
" <td>1.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>3.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1.2</td>\n", | |
" <td>1.1</td>\n", | |
" <td>1.0</td>\n", | |
" <td>3.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2.2</td>\n", | |
" <td>2.2</td>\n", | |
" <td>0.5</td>\n", | |
" <td>3.20</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>1.3</td>\n", | |
" <td>1.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>3.15</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>2.3</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.5</td>\n", | |
" <td>3.50</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>1.1</td>\n", | |
" <td>0.3</td>\n", | |
" <td>1.5</td>\n", | |
" <td>3.00</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>2.1</td>\n", | |
" <td>1.1</td>\n", | |
" <td>1.0</td>\n", | |
" <td>3.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>1.2</td>\n", | |
" <td>2.2</td>\n", | |
" <td>0.5</td>\n", | |
" <td>3.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>2.2</td>\n", | |
" <td>1.1</td>\n", | |
" <td>1.0</td>\n", | |
" <td>3.20</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>10</th>\n", | |
" <td>1.3</td>\n", | |
" <td>1.2</td>\n", | |
" <td>2.1</td>\n", | |
" <td>3.15</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>11</th>\n", | |
" <td>2.3</td>\n", | |
" <td>2.5</td>\n", | |
" <td>1.7</td>\n", | |
" <td>3.50</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>12</th>\n", | |
" <td>1.1</td>\n", | |
" <td>1.1</td>\n", | |
" <td>1.0</td>\n", | |
" <td>3.00</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>13</th>\n", | |
" <td>2.1</td>\n", | |
" <td>0.3</td>\n", | |
" <td>1.5</td>\n", | |
" <td>3.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>14</th>\n", | |
" <td>1.2</td>\n", | |
" <td>0.5</td>\n", | |
" <td>2.2</td>\n", | |
" <td>3.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>15</th>\n", | |
" <td>2.2</td>\n", | |
" <td>1.2</td>\n", | |
" <td>2.1</td>\n", | |
" <td>3.20</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>16</th>\n", | |
" <td>1.3</td>\n", | |
" <td>2.5</td>\n", | |
" <td>1.7</td>\n", | |
" <td>3.15</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>17</th>\n", | |
" <td>2.3</td>\n", | |
" <td>1.2</td>\n", | |
" <td>2.1</td>\n", | |
" <td>3.50</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>18</th>\n", | |
" <td>1.1</td>\n", | |
" <td>1.3</td>\n", | |
" <td>3.2</td>\n", | |
" <td>3.00</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>19</th>\n", | |
" <td>2.1</td>\n", | |
" <td>2.7</td>\n", | |
" <td>2.8</td>\n", | |
" <td>3.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>20</th>\n", | |
" <td>1.2</td>\n", | |
" <td>1.2</td>\n", | |
" <td>2.1</td>\n", | |
" <td>3.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>21</th>\n", | |
" <td>2.2</td>\n", | |
" <td>0.5</td>\n", | |
" <td>2.2</td>\n", | |
" <td>3.20</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>22</th>\n", | |
" <td>1.3</td>\n", | |
" <td>0.6</td>\n", | |
" <td>3.3</td>\n", | |
" <td>3.15</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>23</th>\n", | |
" <td>2.3</td>\n", | |
" <td>1.3</td>\n", | |
" <td>3.2</td>\n", | |
" <td>3.50</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" iid x y value\n", | |
"0 1.1 2.0 -0.5 3.00\n", | |
"1 2.1 1.0 0.0 3.10\n", | |
"2 1.2 1.1 1.0 3.10\n", | |
"3 2.2 2.2 0.5 3.20\n", | |
"4 1.3 1.0 0.0 3.15\n", | |
"5 2.3 0.0 0.5 3.50\n", | |
"6 1.1 0.3 1.5 3.00\n", | |
"7 2.1 1.1 1.0 3.10\n", | |
"8 1.2 2.2 0.5 3.10\n", | |
"9 2.2 1.1 1.0 3.20\n", | |
"10 1.3 1.2 2.1 3.15\n", | |
"11 2.3 2.5 1.7 3.50\n", | |
"12 1.1 1.1 1.0 3.00\n", | |
"13 2.1 0.3 1.5 3.10\n", | |
"14 1.2 0.5 2.2 3.10\n", | |
"15 2.2 1.2 2.1 3.20\n", | |
"16 1.3 2.5 1.7 3.15\n", | |
"17 2.3 1.2 2.1 3.50\n", | |
"18 1.1 1.3 3.2 3.00\n", | |
"19 2.1 2.7 2.8 3.10\n", | |
"20 1.2 1.2 2.1 3.10\n", | |
"21 2.2 0.5 2.2 3.20\n", | |
"22 1.3 0.6 3.3 3.15\n", | |
"23 2.3 1.3 3.2 3.50" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"datapoly_df = pd.merge(positions_df, values_df, how='left', on='iid' )\n", | |
"print('datapoly_df shape as rows,columns ', datapoly_df.shape)\n", | |
"datapoly_df.head(24)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## From ggplot2 geom_plot example\n", | |
"```\n", | |
"p <- ggplot(datapoly, aes(x = x, y = y)) +\n", | |
" geom_polygon(aes(fill = value, group = id))\n", | |
"p\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"p = (ggplot(aes('x', 'y' )) +\n", | |
" geom_polygon(datapoly_df, aes(fill = '#333333')))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "PlotnineError", | |
"evalue": "\"Could not evaluate the 'fill' mapping: '#333333' (original error: unexpected EOF while parsing (<string>, line 1))\"", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mSyntaxError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m/home/stoney/plotnine/plotnine/layer.py\u001b[0m in \u001b[0;36mcompute_aesthetics\u001b[0;34m(self, plot)\u001b[0m\n\u001b[1;32m 318\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 319\u001b[0;31m \u001b[0mnew_val\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcol\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minner_namespace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 320\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/home/stoney/anaconda3/lib/python3.6/site-packages/patsy/eval.py\u001b[0m in \u001b[0;36meval\u001b[0;34m(self, expr, source_name, inner_namespace)\u001b[0m\n\u001b[1;32m 163\u001b[0m \"\"\"\n\u001b[0;32m--> 164\u001b[0;31m \u001b[0mcode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcompile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexpr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msource_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"eval\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 165\u001b[0m return eval(code, {}, VarLookupDict([inner_namespace]\n", | |
"\u001b[0;31mSyntaxError\u001b[0m: unexpected EOF while parsing (<string>, line 1)", | |
"\nDuring handling of the above exception, another exception occurred:\n", | |
"\u001b[0;31mPlotnineError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-15-568c012e485d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdraw\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m/home/stoney/plotnine/plotnine/ggplot.py\u001b[0m in \u001b[0;36mdraw\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[0;31m# assign a default theme\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 139\u001b[0m \u001b[0mself\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdeepcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 140\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_build\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 141\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 142\u001b[0m \u001b[0;31m# If no theme we use the default\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/home/stoney/plotnine/plotnine/ggplot.py\u001b[0m in \u001b[0;36m_build\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 223\u001b[0m \u001b[0;31m# Compute aesthetics to produce data with generalised\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 224\u001b[0m \u001b[0;31m# variable names\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 225\u001b[0;31m \u001b[0mlayers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute_aesthetics\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 226\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 227\u001b[0m \u001b[0;31m# Transform data using all scales\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/home/stoney/plotnine/plotnine/layer.py\u001b[0m in \u001b[0;36mcompute_aesthetics\u001b[0;34m(self, plot)\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompute_aesthetics\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mplot\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 89\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ml\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 90\u001b[0;31m \u001b[0ml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute_aesthetics\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 91\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 92\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompute_statistic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlayout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/home/stoney/plotnine/plotnine/layer.py\u001b[0m in \u001b[0;36mcompute_aesthetics\u001b[0;34m(self, plot)\u001b[0m\n\u001b[1;32m 320\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 321\u001b[0m raise PlotnineError(\n\u001b[0;32m--> 322\u001b[0;31m _TPL_EVAL_FAIL.format(ae, col, str(e)))\n\u001b[0m\u001b[1;32m 323\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 324\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mPlotnineError\u001b[0m: \"Could not evaluate the 'fill' mapping: '#333333' (original error: unexpected EOF while parsing (<string>, line 1))\"" | |
] | |
} | |
], | |
"source": [ | |
"p.draw()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"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.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment