Best plotly equivalent to sns.distplot(data, fit=norm)

Hello there.

I’m trying to find the best, quickest equivalent possible to the following seaborn snippet:

import seaborn as sns
from scipy.stats import norm

sns.distplot(data, fit=norm)

This allows me to fit the normal to an existent displot in seaborn in a very handy manner. What would be the best equivalent in plotly?

Thanks for your time

Hi @jralfonsog,

Welcome to Plotly forum!!
Plotly provides the function plotly.figure_factory.create_distplot() to generate a distplot, that can display the histogram, the pdf estimate, and the rug plot:

create_distplot(hist_data, group_labels, bin_size=1.0, curve_type='kde', colors=None, rug_text=None, histnorm='probability density', show_hist=True, show_curve=True, show_rug=True)

This function works with multiple data sets. If you want to plot just the distplot associated to a single sample,
x= [n values], then pass to hist_data, [x], i.e. a list of a list, not just x.

Example:

import plotly.figure_factory as ff
import numpy as np
np.random.seed(123)
x = np.random.normal(loc=2.5, scale=0.85, size=300) 
group_labels = 'My sample'

# Create distplot with custom bin_size, and without rug plot
fig = ff.create_distplot([x], [group_labels], bin_size=.2, show_rug=False)
fig.update_layout(width=600, 
                  height=400,
                  bargap=0.01)

distplot1

If we set above, show_rug=True, we get:

distplotRug

For more information on this function type:

help(ff.create_distplot)

and here https://plot.ly/python/distplot/ you can find more examples, but with no settings to ensure plot aesthetics (i.e. they are plotted with default layout.width and layout.height, and the bargap is not set, as i did above). That’s why the histograms look like a continuum, not like in these seaborn examples https://seaborn.pydata.org/generated/seaborn.distplot.html.
Hence you should customize the figure appearance.

1 Like