{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"from plotly.graph_objs import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Some data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = ['TRUE', 'TRUE', 'TRUE', 'TRUE', 'TRUE', 'TRUE',\n",
" 'FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE']\n",
"\n",
"y_noClassGc = [0.2, 0.2, 0.6, 1.0, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3]\n",
"\n",
"y_aggresiveOpts = [0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Box plot (version 1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trace0 = Box(\n",
" y=y_noClassGc,\n",
" x=x,\n",
" name='noClassGc',\n",
" marker=Marker(\n",
" color='#3D9970'\n",
" )\n",
")\n",
"trace1 = Box(\n",
" y=y_aggresiveOpts,\n",
" x=x,\n",
" name='aggresiveOpts',\n",
" marker=Marker(\n",
" color='#FF4136'\n",
" )\n",
")\n",
"\n",
"data = Data([trace0, trace1])\n",
"layout = Layout(\n",
" yaxis=YAxis(\n",
" title='confidence',\n",
" zeroline=False\n",
" ),\n",
" boxmode='group'\n",
")\n",
"\n",
"fig = Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='box-grouped')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Above, the two traces share the same `'x'` coordinates. At a given `'x'` coordinate, there are as many box plot as there are traces. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Manipulate the data\n",
"\n",
"To plot the desired result, instead of sending traces delimited by category (`'noClassGc'` and `'aggresiveOpts'`), send traces delimited by `'TRUE'` / `'FALSE'` values.\n",
"\n",
"So, generate two new lists of `'y'` sample points, one for `'TRUE'` and one for `'FALSE'`:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.2, 0.2),\n",
" (0.6, 0.7),\n",
" (0.2, 0.7),\n",
" (0.7, 0.9),\n",
" (0.6, 0.9),\n",
" (0.3, 0.5),\n",
" (1.0, 0.1),\n",
" (0.6, 0.8),\n",
" (0.5, 0.5),\n",
" (0.0, 0.7),\n",
" (0.4, 0.3),\n",
" (0.5, 0.2)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_true = []\n",
"y_false = []\n",
"\n",
"for xi, yi_noClassGc, yi_aggresiveOpts in zip(x, y_noClassGc, y_aggresiveOpts):\n",
" if xi == 'TRUE':\n",
" y_true.append(yi_noClassGc)\n",
" y_true.append(yi_aggresiveOpts)\n",
" else:\n",
" y_false.append(yi_noClassGc)\n",
" y_false.append(yi_aggresiveOpts)\n",
" \n",
"zip(y_true, y_false)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate the `'x'` coordinates corresponding to the two categories:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['noClassGc',\n",
" 'noClassGc',\n",
" 'noClassGc',\n",
" 'noClassGc',\n",
" 'noClassGc',\n",
" 'noClassGc',\n",
" 'aggresiveOpts',\n",
" 'aggresiveOpts',\n",
" 'aggresiveOpts',\n",
" 'aggresiveOpts',\n",
" 'aggresiveOpts',\n",
" 'aggresiveOpts']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x2 = ['noClassGc'] * (len(y_noClassGc) / 2) + ['aggresiveOpts'] * (len(y_aggresiveOpts) / 2)\n",
"x2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Box plot (version 2)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trace0 = Box(\n",
" y=y_true,\n",
" x=x2,\n",
" name='TRUE',\n",
" marker=Marker(\n",
" color='#3D9970'\n",
" )\n",
")\n",
"trace1 = Box(\n",
" y=y_false,\n",
" x=x2,\n",
" name='FALSE',\n",
" marker=Marker(\n",
" color='#FF4136'\n",
" )\n",
")\n",
"\n",
"data = Data([trace0, trace1])\n",
"layout = Layout(\n",
" yaxis=YAxis(\n",
" title='confidence',\n",
" zeroline=False\n",
" ),\n",
" boxmode='group'\n",
")\n",
"\n",
"fig = Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='box-grouped2')"
]
}
],
"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
}