plotly

Plotly.express.ecdf

“Empirical Cumulative Distribution Function or ECDF plots are types of plots that allow you to visualize the distribution of numerical data. A cumulative distribution function is a step function that jumps up by 1/n at each of n data points.

You can learn more about ECDF in the resource below:

https://en.wikipedia.org/wiki/Empirical_distribution_function

For this article, we will focus on creating ECDF plots using the Plotly Express module.”

Simple ECDF Plot

The following code shows how to create a simple ECDF Plot using the Plotly express module.

import plotly.express as px

df = px.data.tips()

fig = px.ecdf(df, x='total_bill')

fig.show()

The code above should return an ECDF distribution plot as shown:

To create combined ECDF plots, you can specify the columns you wish to plot as a list. An example code is shown below:

import plotly.express as px

df = px.data.tips()

fig = px.ecdf(df, x=['total_bill', 'tip'])

fig.show()

The output figure:

To plot the color dimension in the plot, we can run:

import plotly.express as px

df = px.data.tips()

fig = px.ecdf(df, x='total_bill', color='day')

fig.show()

Output:

To show the raw counts of the data, set the ecdfnorm parameter to None as shown:

import plotly.express as px

df = px.data.tips()

fig = px.ecdf(df, x='total_bill', color='day', ecdfnorm=None)

fig.show()

Resulting plot:

You can also customize the orientation of the ECDF plot by setting the orientation parameter.

For example, to set the orientation to horizontal, run:

import plotly.express as px

df = px.data.tips()

fig = px.ecdf(df, x='total_bill', y='tip', color='day', orientation='h', ecdfnorm=None)

fig.show()

The code above should return the ECDF plot in horizontal orientation as:

Change the orientation value to “v” to create a vertically oriented ECDF plot.

To include markers in your ECDF plot, use the markers parameter as:

import plotly.express as px

df = px.data.tips()

fig = px.ecdf(df, x='total_bill', y='tip', color='day', orientation='h', ecdfnorm=None, markers=True)

fig.show()

The resulting figure:

To show a marginal plot, we the marginal parameter to the type of plot you wish to include:

import plotly.express as px

df = px.data.tips()

fig = px.ecdf(df, x='total_bill', y='tip', color='day', orientation='h', ecdfnorm=None, markers=True, marginal='box')

fig.show()

The code above will include a box plot as a marginal plot as:

To show a facetted ECDF plot, specify the facet_col parameter as:

import plotly.express as px

df = px.data.tips()

fig = px.ecdf(df, x='total_bill', y='tip', color='day', orientation='h', ecdfnorm=None, markers=True, facet_col='day')

fig.show()

Output:

And that’s it.

Closing

In this article, we covered how to create various types of ECDF plots using the Plotly Express module.

Happy coding!!

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