#!/usr/bin/env python # coding: utf-8 # #### New to Plotly? # 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/). #
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). #
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started! # #### Version Check # Note: Violin Plots are available in version 1.12.1+
# Run `pip install plotly --upgrade` to update your Plotly version. # In[1]: import plotly plotly.__version__ # #### One Violin # In[2]: import plotly.plotly as py import plotly.figure_factory as ff import plotly.graph_objs as go import numpy as np from scipy import stats data_list = np.random.randn(100) data_list.tolist() fig = ff.create_violin(data_list, colors='#604d9e') py.iplot(fig, filename='One Violin') # #### Multiple Violins # In[3]: import plotly.plotly as py import plotly.figure_factory as ff import plotly.graph_objs as go import numpy as np import pandas as pd from scipy import stats np.random.seed(619517) Nr = 250 y = np.random.randn(Nr) gr = np.random.choice(list("ABCDE"), Nr) norm_params = [(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)] for i, letter in enumerate("ABCDE"): y[gr == letter] *= norm_params[i][1] + norm_params[i][0] df = pd.DataFrame(dict(Score = y, Group = gr)) fig = ff.create_violin(df, data_header='Score', group_header='Group', height=500, width=800) py.iplot(fig, filename='Multiple Violins') # #### Violin Plots with Colorscale # In[4]: import plotly.plotly as py import plotly.figure_factory as ff import plotly.graph_objs as go import numpy as np import pandas as pd from scipy import stats np.random.seed(619517) Nr = 250 y = np.random.randn(Nr) gr = np.random.choice(list("ABCDE"), Nr) norm_params = [(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)] for i, letter in enumerate("ABCDE"): y[gr == letter] *= norm_params[i][1] + norm_params[i][0] df = pd.DataFrame(dict(Score = y, Group = gr)) data_header = 'Score' group_header = 'Group' group_stats = {} groupby_data = df.groupby([group_header]) for group in "ABCDE": data_from_group = groupby_data.get_group(group)[data_header] stat = np.median(data_from_group) group_stats[group] = stat fig = ff.create_violin(df, data_header='Score', group_header='Group', colors='YlOrRd', height=500, width=800, use_colorscale=True, group_stats=group_stats) py.iplot(fig, filename='Violin Plots with Colorscale') # #### Violin Plots with Dictionary Colors # In[5]: import plotly.plotly as py import plotly.figure_factory as ff import plotly.graph_objs as go import numpy as np import pandas as pd from scipy import stats np.random.seed(619517) Nr = 250 y = np.random.randn(Nr) gr = np.random.choice(list("ABCDE"), Nr) norm_params = [(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)] for i, letter in enumerate("ABCDE"): y[gr == letter] *= norm_params[i][1] + norm_params[i][0] df = pd.DataFrame(dict(Score = y, Group = gr)) data_header = 'Score' group_header = 'Group' colors_dict = dict(A = 'rgb(25, 200, 120)', B = '#aa6ff60', C = (0.3, 0.7, 0.3), D = 'rgb(175, 25, 122)', E = 'rgb(255, 150, 226)') fig = ff.create_violin(df, data_header='Score', group_header='Group', colors=colors_dict, height=500, width=800, use_colorscale=False) py.iplot(fig, filename='Violin Plots with Dictionary Colors') # #### Reference # In[7]: help(ff.create_violin) # In[8]: from IPython.display import display, HTML display(HTML('')) display(HTML('')) get_ipython().system(' pip install git+https://github.com/plotly/publisher.git --upgrade') import publisher publisher.publish( 'violin-plot.ipynb', 'python/legacy/violin-plot/', 'Violin Plots [Legacy]', '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.', title='Python Violin Plots | plotly', name='Violin Plots', thumbnail='thumbnail/violin-plot.jpg', language='python', has_thumbnail='true', display_as='legacy_charts', order=2, ipynb= '~notebook_demo/26') # In[ ]: