How to Create Distplots in Plotly Python?

Spread the love

Distplots –

The distplots in plotly python combines various statistical distribution such as histograms, violin plot, box plot etc in the same plot.

To Create a distplots in plotly python, we can either use the px.histogram function from plotly express or use the create_distplot from the plotly figure_factory.

Let’s read a dataset to work with.

import pandas as pd
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/batting.csv"
df = pd.read_csv(url)
df.head()

Create Distplots using plotly Express –

To create distplots in plotly express, we can use the px.histogram function.

import plotly.express as px
fig = px.histogram(df, x="Runs", color="Nationality",
                   marginal="rug")
fig.show()

Instead of adding a rug to the plot, we can add a box plot or a violin plot.

import plotly.express as px
fig = px.histogram(df, x="Runs", color="Nationality",
                   marginal="box")
fig.show()
import plotly.express as px
fig = px.histogram(df, x="Runs", color="Nationality",
                   marginal="violin")
fig.show()

Create Distplots using create_distplot from Plotly Figure Factory-

You can also create a distplot using the ff.create_distplot from the plotly figure factory.

import plotly.figure_factory as ff
group_labels = ['Runs'] # name of the dataset
fig = ff.create_distplot([df['Runs']], group_labels)
fig.show()

If you plotting a single histogram them make sure to pass it as a list of list as this function is written to handle multiple datasets. Just wrap the column in a list as shown above.

Plot Multiple Datasets –

You can also show multiple histogram using the create_distplot. Let’s create two separate dataframe each of the nationality.

import plotly.figure_factory as ff

# create separate dataframe for each nationality
indian_df = df[df['Nationality']=='Indian']
overseas_df = df[df['Nationality']=='Overseas']

# Group data together
hist_data = [indian_df['Runs'], overseas_df['Runs']]
group_labels = ['Indian','Overseas']

# Create distplot
fig = ff.create_distplot(hist_data, group_labels)
fig.show()

Customize the Color –

You can also change the color of the histograms using the colors parameter.

# define colors
colors = ['blue','green']
# Create distplot
fig = ff.create_distplot(hist_data, group_labels, colors=colors)
fig.show()

Add a Title –

To add a title to the plot, you can use the title parameter of the update_layout method.

# define colors
colors = ['blue','green']
# Create distplot
fig = ff.create_distplot(hist_data, group_labels, colors=colors)
fig.update_layout(title='Runs by Nationality')
fig.show()

Plot Normal Curve –

By Default create_distplot plots a kde plot but you can use a normal curve instead using the curve_type parameter.

# define colors
colors = ['blue','green']
# Create distplot
fig = ff.create_distplot(hist_data, group_labels, 
                         colors=colors,
                        curve_type='normal')
fig.update_layout(title='Runs by Nationality')
fig.show()

Plot Only Curve and Rug –

You can also only show the curve and rug by setting the show_hist=False.

# Create distplot
fig = ff.create_distplot(hist_data, group_labels, 
                         colors=colors,
                        curve_type='normal',
                        show_hist=False)
fig.update_layout(title='Runs by Nationality')
fig.show()

Plot only Hist and Rug –

But if you only want to show histogram and the rug then set show_curve=False.

# Create distplot
fig = ff.create_distplot(hist_data, group_labels, 
                         colors=colors,
                        curve_type='normal',
                        show_curve=False)
fig.update_layout(title='Runs by Nationality')
fig.show()

Plot only Hist and Curve –

If you only want to show a histogram and the curve then set show_rug=False.

# Create distplot
fig = ff.create_distplot(hist_data, group_labels, 
                         colors=colors,
                        curve_type='normal',
                        show_rug=False)
fig.update_layout(title='Runs by Nationality')
fig.show()

Related Posts –

  1. How to install plotly python with pip?
  2. How to create a Line Chart with Plotly Python?
  3. How to create Scatter plot in Plotly Python?
  4. How to create a Bar Chart in Plotly Python?
  5. How to create Horizontal Bar Chart in Plotly Python?
  6. How to create a Histogram in plotly python?
  7. How to Create a Box Plot in Plotly Python?
  8. How to create a Pie Chart in Plotly Python?
  9. How to create a Dot Plot in Plotly Python?
  10. How to Create Heatmap with Plotly Python?
  11. How to Create a Violin Plot in Plotly Python?
  12. How to Create Subplots in Plotly Python?
  13. How to Create a Bubble Chart in Plotly Python?
  14. How to Create a Gantt Chart in Plotly Python?
  15. How to Create an Area Chart in Plotly Python?
  16. How to Create Tables in Plotly Python?
  17. How to Create a Sunburst Chart in Plotly Python?
  18. How to Create a Sankey Diagram in Plotly Python?
  19. How to Create a Treemap Charts in Plotly Python?

Rating: 1 out of 5.

Leave a Reply