Violin plot - constrain distribution to within data bounds

Hello all,

I was wondering if there is a way to constrain the distribution shown on a violin plot to be within the bounds of the data that is being plotted. What I am looking for is something like seaborns ‘cut’. Basically, I want to constrain the distribution curve generated by the violin plot to have the same max and min value as my data.

Hi @purpleboy
You can restrict plotting the pdf to a given range, using the attributes span, and spanmode.
span is a list of two values span =[start, end] and spanmode = 'manual'

Example:

import pandas as pd

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

fig = go.Figure(data=go.Violin(y=df['total_bill'],  line_color='black', 
                               meanline_visible=True, fillcolor='lightseagreen', opacity=0.6,
                               x0='Total Bill'))

fig.update_layout(yaxis_zeroline=False, width=500)
fig.show()

fig1-violin
Now update fig.data[0] setting span and spanmode:

fig.data[0].update(span = [10, 40], spanmode='manual');

fig2-violin

three reasons why you should buy plotly pro: support open source, get great support, host your plots and dashboards online