Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to show both a normal distribution and a kernel density estimation in a histogram?

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()
like image 675
vestland Avatar asked Sep 12 '20 21:09

vestland


People also ask

What is the difference between a histogram and a kernel density estimate?

A histogram puts all samples between the boundaries of each bin will fall into the bin. It doesn't differentiate whether the value falls close the left, to the right or the center of the bin. A kde plot, on the other hand, takes each individual sample value and draws a small gaussian bell curve over it.

What is kernel density in histogram?

Description. As known as Kernel Density Plots, Density Trace Graph. A Density Plot visualises the distribution of data over a continuous interval or time period. This chart is a variation of a Histogram that uses kernel smoothing to plot values, allowing for smoother distributions by smoothing out the noise.

What does a KDE plot show?

Kdeplot is a Kernel Distribution Estimation Plot which depicts the probability density function of the continuous or non-parametric data variables i.e. we can plot for the univariate or multiple variables altogether. Using the Python Seaborn module, we can build the Kdeplot with various functionality added to it.

What is kernel density estimate plot in Python?

KDE Plot is known as Kernel Density Estimate Plot which is generally used for estimating the e Probability Density function of a continuous variable. It is a method for visualizing the distribution of observations in a dataset, analogous to a histogram. It represents the data using a continuous probability density curve in one or more dimensions.

How do you plot a normal distribution histogram?

# Plotting the histogram. The normal distribution chart is characterized by two parameters: The average value, which represents the maximum value of the chart, and the chart is always symmetrical. And the standard deviation, which determines the amount of change beyond the mean.

What is a density plot in statistics?

A density plot is a smoothed, continuous version of a histogram estimated from the data. The most common form of estimation is known as kernel density estimation (KDE) . In this method, a continuous curve (the kernel) is drawn at every individual data point and all of these curves are then added together to make a single smooth density estimation.

How to create 2D density plot over histogram data in Python?

The create_2d_density () function in module plotly.figure_factory._2d_density returns a figure object for a 2D density plot. Following code is used to produce 2D Density plot over histogram data.


Video Answer


1 Answers

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

like image 161
vestland Avatar answered Nov 08 '22 01:11

vestland