plotly

Plotly.Figure_Factory.Create_Distplot

In this article, we will explore how to use the Plotly figure_factory module to create distplots.

Let’s dive in.

Function Syntax

The following code snippets illustrate the function syntax and required parameters:

plotly.figure_factory.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)

Parameters:

  1. hist_data – Specifies the histogram data.
  2. group_labels – Specifies the names of the provided data set.
  3. bin_size – Specifies the size of the histogram bins.
  4. curve_type – Sets the type of curve.
  5. histnorm – Defines the function used.
  6. showhist – Determines if the histogram should be visible in the distplot.
  7. colors – Sets the values used for color traces.
  8. show_rug – Determines if a rug should be included in the distplot.

Basic Distplot with Plotly Figure_Factory

The following code illustrates how to create a simple distplot using Plotly figure_factory.

from plotly.figure_factory import create_distplot
import numpy as np

np.random.seed(1)
x=np.random.randn(100)
hist_data =[x]
group_labels = ['distplot basic example']
fig = create_distplot(hist_data, group_labels)
fig.show()

The previous code creates a simple distplot as shown in the following output:

To hide the rug plot, we can use the show_rug parameter as shown in the following example:

from plotly.figure_factory import create_distplot
import numpy as np
np.random.seed(1)
x=np.random.randn(100)
hist_data =[x]
group_labels = ['distplot basic example']
fig = create_distplot(hist_data, group_labels, show_rug=False)
fig.show()

Output Figure:

The given figure disables the rug plot which is included at the bottom of the displot by default.
To add a title to the figure, we can use the update_layout() function as shown in the following code:

fig.update_layout(title='Basic Distplot')
fig.show()

Output:

To create a distplot with a normal curve, we can set the curve_type parameter as shown in the following:

fig = create_distplot(hist_data, group_labels, show_rug=False, curve_type='normal')
You can also create a distplot using Pandas DataFrame and figure_factory module as shown in the following sample code:
from plotly.figure_factory import create_distplot
import numpy as np
import pandas as pd
df = pd.DataFrame({'2012': np.random.randn(200),
                   '2013': np.random.randn(200)+1})
fig = create_distplot([df[c] for c in df.columns], df.columns, bin_size=.25)
fig.show()

The output distplot is as shown in the following:

Conclusion

In this article, we explored the fundamentals of creating the distplots using Plotly figure_factory module.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list