{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version Check\n", "Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.4.0'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic Violin Plot" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n", "\n", "fig = {\n", " \"data\": [{\n", " \"type\": 'violin',\n", " \"y\": df['total_bill'],\n", " \"box\": {\n", " \"visible\": True\n", " },\n", " \"line\": {\n", " \"color\": 'black'\n", " },\n", " \"meanline\": {\n", " \"visible\": True\n", " },\n", " \"fillcolor\": '#8dd3c7',\n", " \"opacity\": 0.6,\n", " \"x0\": 'Total Bill'\n", " }],\n", " \"layout\" : {\n", " \"title\": \"\",\n", " \"yaxis\": {\n", " \"zeroline\": False,\n", " }\n", " }\n", "}\n", "\n", "py.iplot(fig, filename = 'violin/basic', validate = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multiple Traces" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n", "\n", "data = []\n", "for i in range(0,len(pd.unique(df['day']))):\n", " trace = {\n", " \"type\": 'violin',\n", " \"x\": df['day'][df['day'] == pd.unique(df['day'])[i]],\n", " \"y\": df['total_bill'][df['day'] == pd.unique(df['day'])[i]],\n", " \"name\": pd.unique(df['day'])[i],\n", " \"box\": {\n", " \"visible\": True\n", " },\n", " \"meanline\": {\n", " \"visible\": True\n", " }\n", " }\n", " data.append(trace)\n", "\n", " \n", "fig = {\n", " \"data\": data,\n", " \"layout\" : {\n", " \"title\": \"\",\n", " \"yaxis\": {\n", " \"zeroline\": False,\n", " }\n", " }\n", "}\n", "\n", "\n", "py.iplot(fig, filename='violin/multiple', validate = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Grouped Violin Plot" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n", "\n", "fig = {\n", " \"data\": [\n", " {\n", " \"type\": 'violin',\n", " \"x\": df['day'] [ df['sex'] == 'Male' ],\n", " \"y\": df['total_bill'] [ df['sex'] == 'Male' ],\n", " \"legendgroup\": 'M',\n", " \"scalegroup\": 'M',\n", " \"name\": 'M',\n", " \"box\": {\n", " \"visible\": True\n", " },\n", " \"meanline\": {\n", " \"visible\": True\n", " },\n", " \"line\": {\n", " \"color\": 'blue'\n", " }\n", " },\n", " {\n", " \"type\": 'violin',\n", " \"x\": df['day'] [ df['sex'] == 'Female' ],\n", " \"y\": df['total_bill'] [ df['sex'] == 'Female' ],\n", " \"legendgroup\": 'F',\n", " \"scalegroup\": 'F',\n", " \"name\": 'F',\n", " \"box\": {\n", " \"visible\": True\n", " },\n", " \"meanline\": {\n", " \"visible\": True\n", " },\n", " \"line\": {\n", " \"color\": 'pink'\n", " }\n", " }\n", " ],\n", " \"layout\" : {\n", " \"yaxis\": {\n", " \"zeroline\": False,\n", " },\n", " \"violinmode\": \"group\"\n", " }\n", "}\n", "\n", "\n", "py.iplot(fig, filename = 'violin/grouped', validate = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Split Violin Plot" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n", "\n", "fig = {\n", " \"data\": [\n", " {\n", " \"type\": 'violin',\n", " \"x\": df['day'] [ df['smoker'] == 'Yes' ],\n", " \"y\": df['total_bill'] [ df['smoker'] == 'Yes' ],\n", " \"legendgroup\": 'Yes',\n", " \"scalegroup\": 'Yes',\n", " \"name\": 'Yes',\n", " \"side\": 'negative',\n", " \"box\": {\n", " \"visible\": True\n", " },\n", " \"meanline\": {\n", " \"visible\": True\n", " },\n", " \"line\": {\n", " \"color\": 'blue'\n", " }\n", " },\n", " {\n", " \"type\": 'violin',\n", " \"x\": df['day'] [ df['smoker'] == 'No' ],\n", " \"y\": df['total_bill'] [ df['smoker'] == 'No' ],\n", " \"legendgroup\": 'No',\n", " \"scalegroup\": 'No',\n", " \"name\": 'No',\n", " \"side\": 'positive',\n", " \"box\": {\n", " \"visible\": True\n", " },\n", " \"meanline\": {\n", " \"visible\": True\n", " },\n", " \"line\": {\n", " \"color\": 'green'\n", " }\n", " }\n", " ],\n", " \"layout\" : {\n", " \"yaxis\": {\n", " \"zeroline\": False,\n", " },\n", " \"violingap\": 0,\n", " \"violinmode\": \"overlay\"\n", " }\n", "}\n", "\n", "\n", "py.iplot(fig, filename = 'violin/split', validate = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Advanced Violin Plot" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n", "\n", "pointposMale = [-0.9,-1.1,-0.6,-0.3]\n", "pointposFemale = [0.45,0.55,1,0.4]\n", "showLegend = [True,False,False,False]\n", "\n", "data = []\n", "for i in range(0,len(pd.unique(df['day']))):\n", " male = {\n", " \"type\": 'violin',\n", " \"x\": df['day'][ (df['sex'] == 'Male') & (df['day'] == pd.unique(df['day'])[i]) ],\n", " \"y\": df['total_bill'][ (df['sex'] == 'Male') & (df['day'] == pd.unique(df['day'])[i]) ],\n", " \"legendgroup\": 'M',\n", " \"scalegroup\": 'M',\n", " \"name\": 'M',\n", " \"side\": 'negative',\n", " \"box\": {\n", " \"visible\": True\n", " },\n", " \"points\": 'all',\n", " \"pointpos\": pointposMale[i],\n", " \"jitter\": 0,\n", " \"scalemode\": 'count',\n", " \"meanline\": {\n", " \"visible\": True\n", " },\n", " \"line\": {\n", " \"color\": '#8dd3c7'\n", " },\n", " \"marker\": {\n", " \"line\": {\n", " \"width\": 2,\n", " \"color\": '#8dd3c7'\n", " }\n", " },\n", " \"span\": [\n", " 0\n", " ],\n", " \"showlegend\": showLegend[i]\n", " }\n", " data.append(male)\n", " female = {\n", " \"type\": 'violin',\n", " \"x\": df['day'] [ (df['sex'] == 'Female') & (df['day'] == pd.unique(df['day'])[i]) ],\n", " \"y\": df['total_bill'] [ (df['sex'] == 'Female') & (df['day'] == pd.unique(df['day'])[i]) ],\n", " \"legendgroup\": 'F',\n", " \"scalegroup\": 'F',\n", " \"name\": 'F',\n", " \"side\": 'positive',\n", " \"box\": {\n", " \"visible\": True\n", " },\n", " \"points\": 'all',\n", " \"pointpos\": pointposFemale[i],\n", " \"jitter\": 0,\n", " \"scalemode\": 'count',\n", " \"meanline\": {\n", " \"visible\": True\n", " },\n", " \"line\": {\n", " \"color\": '#bebada'\n", " },\n", " \"marker\": {\n", " \"line\": {\n", " \"width\": 2,\n", " \"color\": '#bebada'\n", " }\n", " },\n", " \"span\": [\n", " 0\n", " ],\n", " \"showlegend\": showLegend[i]\n", " }\n", " data.append(female)\n", " \n", "\n", "fig = {\n", " \"data\": data,\n", " \"layout\" : {\n", " \"title\": \"Total bill distribution
scaled by number of bills per gender\",\n", " \"yaxis\": {\n", " \"zeroline\": False,\n", " },\n", " \"violingap\": 0,\n", " \"violingroupgap\": 0,\n", " \"violinmode\": \"overlay\"\n", " }\n", "}\n", "\n", "\n", "py.iplot(fig, filename='violin/advanced', validate = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#violin for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to c:\\users\\branden\\appdata\\local\\temp\\pip-5beb6u-build\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", " Running setup.py install for publisher: started\n", " Running setup.py install for publisher: finished with status 'done'\n", "Successfully installed publisher-0.11\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'violin.ipynb', 'python/violin/', 'Violin Plots',\n", " 'How to make violin plots in Python with Plotly.',\n", " title = 'Violin Plots | Plotly',\n", " has_thumbnail='true', \n", " thumbnail='thumbnail/violin.jpg', \n", " language='python', \n", " display_as='statistical', \n", " order=12, \n", " ipynb='~notebook_demo/201')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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.12" } }, "nbformat": 4, "nbformat_minor": 2 }