4
votes

For a plotly figure factory distribution plot, the default distribution is kde (kernel density estimation):

enter image description here

You can override the default by setting curve = 'normal' to get:

enter image description here

But how can you show both kde and the normal curve in the same plot? Assigning a list like curve_type = ['kde', 'normal'] will not work.

Complete code:

import plotly.figure_factory as ff
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
np.random.seed(2)

x = np.random.randn(1000)
hist_data = [x]
group_labels = ['distplot'] # name of the dataset

mean = np.mean(x)
stdev_pluss = np.std(x)
stdev_minus = np.std(x)*-1

fig = ff.create_distplot(hist_data, group_labels, curve_type='kde')
fig.update_layout(template = 'plotly_dark')
fig.show()
1

1 Answers

6
votes

The easiest thing to do is build another figure fig2 with curve_type = 'normal' and pick up the values from there using:

fig2 = ff.create_distplot(hist_data, group_labels, curve_type = 'normal')
normal_x = fig2.data[1]['x']
normal_y = fig2.data[1]['y']

And then inlclude those values in the first fig using fid.add_trace(go.Scatter()) like this:

fig2 = ff.create_distplot(hist_data, group_labels, curve_type = 'normal')
normal_x = fig2.data[1]['x']
normal_y = fig2.data[1]['y']
fig.add_traces(go.Scatter(x=normal_x, y=normal_y, mode = 'lines',
                          line = dict(color='rgba(0,255,0, 0.6)',
                                      #dash = 'dash'
                                      width = 1),
                          name = 'normal'
                         ))
fig.show()

Plot with two density curves:

enter image description here