Show Sidebar Hide Sidebar

Violin Plots in Python

How to make violin plots in Python with Plotly.

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

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [2]:
import plotly
plotly.__version__
Out[2]:
'2.4.0'

Basic Violin Plot

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

fig = {
    "data": [{
        "type": 'violin',
        "y": df['total_bill'],
        "box": {
            "visible": True
        },
        "line": {
            "color": 'black'
        },
        "meanline": {
            "visible": True
        },
        "fillcolor": '#8dd3c7',
        "opacity": 0.6,
        "x0": 'Total Bill'
    }],
    "layout" : {
        "title": "",
        "yaxis": {
            "zeroline": False,
        }
    }
}

py.iplot(fig, filename = 'violin/basic', validate = False)
Out[3]:

Multiple Traces

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

data = []
for i in range(0,len(pd.unique(df['day']))):
    trace = {
            "type": 'violin',
            "x": df['day'][df['day'] == pd.unique(df['day'])[i]],
            "y": df['total_bill'][df['day'] == pd.unique(df['day'])[i]],
            "name": pd.unique(df['day'])[i],
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            }
        }
    data.append(trace)

        
fig = {
    "data": data,
    "layout" : {
        "title": "",
        "yaxis": {
            "zeroline": False,
        }
    }
}


py.iplot(fig, filename='violin/multiple', validate = False)
Out[4]:

Grouped Violin Plot

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

fig = {
    "data": [
        {
            "type": 'violin',
            "x": df['day'] [ df['sex'] == 'Male' ],
            "y": df['total_bill'] [ df['sex'] == 'Male' ],
            "legendgroup": 'M',
            "scalegroup": 'M',
            "name": 'M',
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            },
            "line": {
                "color": 'blue'
            }
        },
        {
            "type": 'violin',
            "x": df['day'] [ df['sex'] == 'Female' ],
            "y": df['total_bill'] [ df['sex'] == 'Female' ],
            "legendgroup": 'F',
            "scalegroup": 'F',
            "name": 'F',
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            },
            "line": {
                "color": 'pink'
            }
        }
    ],
    "layout" : {
        "yaxis": {
            "zeroline": False,
        },
        "violinmode": "group"
    }
}


py.iplot(fig, filename = 'violin/grouped', validate = False)
Out[5]:

Split Violin Plot

In [6]:
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

fig = {
    "data": [
        {
            "type": 'violin',
            "x": df['day'] [ df['smoker'] == 'Yes' ],
            "y": df['total_bill'] [ df['smoker'] == 'Yes' ],
            "legendgroup": 'Yes',
            "scalegroup": 'Yes',
            "name": 'Yes',
            "side": 'negative',
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            },
            "line": {
                "color": 'blue'
            }
        },
        {
            "type": 'violin',
            "x": df['day'] [ df['smoker'] == 'No' ],
            "y": df['total_bill'] [ df['smoker'] == 'No' ],
            "legendgroup": 'No',
            "scalegroup": 'No',
            "name": 'No',
            "side": 'positive',
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            },
            "line": {
                "color": 'green'
            }
        }
    ],
    "layout" : {
        "yaxis": {
            "zeroline": False,
        },
        "violingap": 0,
        "violinmode": "overlay"
    }
}


py.iplot(fig, filename = 'violin/split', validate = False)
Out[6]:

Advanced Violin Plot

In [7]:
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

pointposMale = [-0.9,-1.1,-0.6,-0.3]
pointposFemale = [0.45,0.55,1,0.4]
showLegend = [True,False,False,False]

data = []
for i in range(0,len(pd.unique(df['day']))):
    male = {
            "type": 'violin',
            "x": df['day'][ (df['sex'] == 'Male') & (df['day'] == pd.unique(df['day'])[i]) ],
            "y": df['total_bill'][ (df['sex'] == 'Male') & (df['day'] == pd.unique(df['day'])[i]) ],
            "legendgroup": 'M',
            "scalegroup": 'M',
            "name": 'M',
            "side": 'negative',
            "box": {
                "visible": True
            },
            "points": 'all',
            "pointpos": pointposMale[i],
            "jitter": 0,
            "scalemode": 'count',
            "meanline": {
                "visible": True
            },
            "line": {
                "color": '#8dd3c7'
            },
            "marker": {
                "line": {
                    "width": 2,
                    "color": '#8dd3c7'
                }
            },
            "span": [
                0
            ],
            "showlegend": showLegend[i]
        }
    data.append(male)
    female = {
            "type": 'violin',
            "x": df['day'] [ (df['sex'] == 'Female') & (df['day'] == pd.unique(df['day'])[i]) ],
            "y": df['total_bill'] [ (df['sex'] == 'Female') & (df['day'] == pd.unique(df['day'])[i]) ],
            "legendgroup": 'F',
            "scalegroup": 'F',
            "name": 'F',
            "side": 'positive',
            "box": {
                "visible": True
            },
            "points": 'all',
            "pointpos": pointposFemale[i],
            "jitter": 0,
            "scalemode": 'count',
            "meanline": {
                "visible": True
            },
            "line": {
                "color": '#bebada'
            },
            "marker": {
                "line": {
                    "width": 2,
                    "color": '#bebada'
                }
            },
            "span": [
                0
            ],
            "showlegend": showLegend[i]
        }
    data.append(female)
        

fig = {
    "data": data,
    "layout" : {
        "title": "Total bill distribution<br><i>scaled by number of bills per gender",
        "yaxis": {
            "zeroline": False,
        },
        "violingap": 0,
        "violingroupgap": 0,
        "violinmode": "overlay"
    }
}


py.iplot(fig, filename='violin/advanced', validate = False)
Out[7]:

Reference

See https://plot.ly/python/reference/#violin for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.