{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/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", "Note: Violin Plots are available in version 1.12.1+
\n", "Run `pip install plotly --upgrade` to update your Plotly version." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.4.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### One Violin" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "from scipy import stats\n", "\n", "data_list = np.random.randn(100)\n", "data_list.tolist()\n", "\n", "fig = ff.create_violin(data_list, colors='#604d9e')\n", "py.iplot(fig, filename='One Violin')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multiple Violins" ] }, { "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.figure_factory as ff\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "import pandas as pd\n", "from scipy import stats\n", "\n", "np.random.seed(619517)\n", "Nr = 250\n", "y = np.random.randn(Nr)\n", "gr = np.random.choice(list(\"ABCDE\"), Nr)\n", "norm_params = [(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]\n", "\n", "for i, letter in enumerate(\"ABCDE\"):\n", " y[gr == letter] *= norm_params[i][1] + norm_params[i][0]\n", "df = pd.DataFrame(dict(Score = y, Group = gr))\n", "\n", "fig = ff.create_violin(df, data_header='Score', group_header='Group',\n", " height=500, width=800)\n", "py.iplot(fig, filename='Multiple Violins')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Violin Plots with Colorscale" ] }, { "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.figure_factory as ff\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "import pandas as pd\n", "from scipy import stats\n", "\n", "np.random.seed(619517)\n", "Nr = 250\n", "y = np.random.randn(Nr)\n", "gr = np.random.choice(list(\"ABCDE\"), Nr)\n", "norm_params = [(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]\n", "\n", "for i, letter in enumerate(\"ABCDE\"):\n", " y[gr == letter] *= norm_params[i][1] + norm_params[i][0]\n", "df = pd.DataFrame(dict(Score = y, Group = gr))\n", "\n", "data_header = 'Score'\n", "group_header = 'Group'\n", "\n", "group_stats = {}\n", "groupby_data = df.groupby([group_header])\n", "\n", "for group in \"ABCDE\":\n", " data_from_group = groupby_data.get_group(group)[data_header]\n", " stat = np.median(data_from_group)\n", " group_stats[group] = stat\n", "\n", "fig = ff.create_violin(df, data_header='Score', group_header='Group',\n", " colors='YlOrRd', height=500, width=800,\n", " use_colorscale=True, group_stats=group_stats)\n", "py.iplot(fig, filename='Violin Plots with Colorscale')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Violin Plots with Dictionary Colors" ] }, { "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.figure_factory as ff\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "import pandas as pd\n", "from scipy import stats\n", "\n", "np.random.seed(619517)\n", "Nr = 250\n", "y = np.random.randn(Nr)\n", "gr = np.random.choice(list(\"ABCDE\"), Nr)\n", "norm_params = [(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]\n", "\n", "for i, letter in enumerate(\"ABCDE\"):\n", " y[gr == letter] *= norm_params[i][1] + norm_params[i][0]\n", "df = pd.DataFrame(dict(Score = y, Group = gr))\n", "\n", "data_header = 'Score'\n", "group_header = 'Group'\n", "\n", "colors_dict = dict(A = 'rgb(25, 200, 120)',\n", " B = '#aa6ff60',\n", " C = (0.3, 0.7, 0.3),\n", " D = 'rgb(175, 25, 122)',\n", " E = 'rgb(255, 150, 226)')\n", "\n", "fig = ff.create_violin(df, data_header='Score', group_header='Group',\n", " colors=colors_dict, height=500, width=800,\n", " use_colorscale=False)\n", "py.iplot(fig, filename='Violin Plots with Dictionary Colors')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function create_violin in module plotly.figure_factory._violin:\n", "\n", "create_violin(data, data_header=None, group_header=None, colors=None, use_colorscale=False, group_stats=None, rugplot=True, sort=False, height=450, width=600, title='Violin and Rug Plot')\n", " Returns figure for a violin plot\n", " \n", " :param (list|array) data: accepts either a list of numerical values,\n", " a list of dictionaries all with identical keys and at least one\n", " column of numeric values, or a pandas dataframe with at least one\n", " column of numbers.\n", " :param (str) data_header: the header of the data column to be used\n", " from an inputted pandas dataframe. Not applicable if 'data' is\n", " a list of numeric values.\n", " :param (str) group_header: applicable if grouping data by a variable.\n", " 'group_header' must be set to the name of the grouping variable.\n", " :param (str|tuple|list|dict) colors: either a plotly scale name,\n", " an rgb or hex color, a color tuple, a list of colors or a\n", " dictionary. An rgb color is of the form 'rgb(x, y, z)' where\n", " x, y and z belong to the interval [0, 255] and a color tuple is a\n", " tuple of the form (a, b, c) where a, b and c belong to [0, 1].\n", " If colors is a list, it must contain valid color types as its\n", " members.\n", " :param (bool) use_colorscale: only applicable if grouping by another\n", " variable. Will implement a colorscale based on the first 2 colors\n", " of param colors. This means colors must be a list with at least 2\n", " colors in it (Plotly colorscales are accepted since they map to a\n", " list of two rgb colors). Default = False\n", " :param (dict) group_stats: a dictioanry where each key is a unique\n", " value from the group_header column in data. Each value must be a\n", " number and will be used to color the violin plots if a colorscale\n", " is being used.\n", " :param (bool) rugplot: determines if a rugplot is draw on violin plot.\n", " Default = True\n", " :param (bool) sort: determines if violins are sorted\n", " alphabetically (True) or by input order (False). Default = False\n", " :param (float) height: the height of the violin plot.\n", " :param (float) width: the width of the violin plot.\n", " :param (str) title: the title of the violin plot.\n", " \n", " Example 1: Single Violin Plot\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_violin\n", " from plotly.graph_objs import graph_objs\n", " \n", " import numpy as np\n", " from scipy import stats\n", " \n", " # create list of random values\n", " data_list = np.random.randn(100)\n", " data_list.tolist()\n", " \n", " # create violin fig\n", " fig = create_violin(data_list, colors='#604d9e')\n", " \n", " # plot\n", " py.iplot(fig, filename='Violin Plot')\n", " ```\n", " \n", " Example 2: Multiple Violin Plots with Qualitative Coloring\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_violin\n", " from plotly.graph_objs import graph_objs\n", " \n", " import numpy as np\n", " import pandas as pd\n", " from scipy import stats\n", " \n", " # create dataframe\n", " np.random.seed(619517)\n", " Nr=250\n", " y = np.random.randn(Nr)\n", " gr = np.random.choice(list(\"ABCDE\"), Nr)\n", " norm_params=[(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]\n", " \n", " for i, letter in enumerate(\"ABCDE\"):\n", " y[gr == letter] *=norm_params[i][1]+ norm_params[i][0]\n", " df = pd.DataFrame(dict(Score=y, Group=gr))\n", " \n", " # create violin fig\n", " fig = create_violin(df, data_header='Score', group_header='Group',\n", " sort=True, height=600, width=1000)\n", " \n", " # plot\n", " py.iplot(fig, filename='Violin Plot with Coloring')\n", " ```\n", " \n", " Example 3: Violin Plots with Colorscale\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_violin\n", " from plotly.graph_objs import graph_objs\n", " \n", " import numpy as np\n", " import pandas as pd\n", " from scipy import stats\n", " \n", " # create dataframe\n", " np.random.seed(619517)\n", " Nr=250\n", " y = np.random.randn(Nr)\n", " gr = np.random.choice(list(\"ABCDE\"), Nr)\n", " norm_params=[(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]\n", " \n", " for i, letter in enumerate(\"ABCDE\"):\n", " y[gr == letter] *=norm_params[i][1]+ norm_params[i][0]\n", " df = pd.DataFrame(dict(Score=y, Group=gr))\n", " \n", " # define header params\n", " data_header = 'Score'\n", " group_header = 'Group'\n", " \n", " # make groupby object with pandas\n", " group_stats = {}\n", " groupby_data = df.groupby([group_header])\n", " \n", " for group in \"ABCDE\":\n", " data_from_group = groupby_data.get_group(group)[data_header]\n", " # take a stat of the grouped data\n", " stat = np.median(data_from_group)\n", " # add to dictionary\n", " group_stats[group] = stat\n", " \n", " # create violin fig\n", " fig = create_violin(df, data_header='Score', group_header='Group',\n", " height=600, width=1000, use_colorscale=True,\n", " group_stats=group_stats)\n", " \n", " # plot\n", " py.iplot(fig, filename='Violin Plot with Colorscale')\n", " ```\n", "\n" ] } ], "source": [ "help(ff.create_violin)" ] }, { "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\\brand\\appdata\\local\\temp\\pip-req-build-lighxczi\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" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Python27\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", "\n", "C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:53: UserWarning:\n", "\n", "Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", "\n", "C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:58: UserWarning:\n", "\n", "Your URL has more than 2 parts... are you sure?\n", "\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-plot.ipynb', 'python/legacy/violin-plot/', 'Violin Plots [Legacy]',\n", " 'How to make Violin Plots in Python with Plotly. A Violin Plot is a plot of numeric data with probability distributions drawn on both sides on the plotted data.',\n", " title='Python Violin Plots | plotly',\n", " name='Violin Plots',\n", " thumbnail='thumbnail/violin-plot.jpg', language='python',\n", " has_thumbnail='true', display_as='legacy_charts', order=2,\n", " ipynb= '~notebook_demo/26')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.14" } }, "nbformat": 4, "nbformat_minor": 1 }