Violin Plots in Python/v3
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.
Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.
See our Version 4 Migration Guide for information about how to upgrade.
New to Plotly?¶
Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (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__
Out[1]:
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')
Out[2]:
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')
Out[3]:
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')
Out[4]:
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')
Out[5]:
Reference¶
In [7]:
help(ff.create_violin)