plotly Pareto Chart in Python (Example)

 

Hi! This short tutorial will show you how to build a pareto chart in plotly in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Install & Import plotly & pandas

To install and import plotly and pandas, run the lines of code below in your preferred Python programming IDE or code editor:

# install plotly & pandas
pip install plotly pandas
 
# import plotly & pandas
import plotly.graph_objs as go
 
import pandas as pd

In addition to plotly, we are also installing and importing the Python pandas library in order for us to be able to build and manipulate DataFrames.

So, with plotly and pandas installed and imported into our Python programming environment, we can now go on to create our example dataset.
 

Create Example Dataset

To create and preview our example dataset, run the lines of code below:

data = {
    'Category': ['A', 'B', 'C', 'D', 'E'],
    'Frequency': [20, 15, 25, 10, 30]
}
 
df = pd.DataFrame(data)
 
print(type(df))
 
# <class 'pandas.core.frame.DataFrame'>
 
print(df)
 
#  Category  Frequency
#0        A         20
#1        B         15
#2        C         25
#3        D         10
#4        E         30

In creating the example dataset, we first created a Python dictionary of key-value pairs. Then, we passed the dictionary to pd.DataFrame() method in order to create the example dataset.

Now that we have created the example DataFrame, we can go on to build a pareto chart.
 

Example: Create Interactive Pareto Chart

We will now build a pareto chart using the example dataset created in the previous section of this tutorial. To do so, run the lines of code below:

df_sorted = df.sort_values(by='Frequency', ascending=False)
 
df_sorted['Cumulative Percentage'] = (df_sorted['Frequency'].cumsum() / df_sorted['Frequency'].sum()) * 100
 
fig = go.Figure()
 
fig.add_trace(go.Bar(
    x=df_sorted['Category'],
    y=df_sorted['Frequency'],
    name='Frequency',
    marker_color='blue'
))
 
fig.add_trace(go.Scatter(
    x=df_sorted['Category'],
    y=df_sorted['Cumulative Percentage'],
    mode='lines+markers',
    name='Cumulative Percentage',
    yaxis='y2',
    marker_color='red'
))
 
fig.update_layout(
    title='Pareto Chart',
    yaxis=dict(title='Frequency'),
    yaxis2=dict(
        title='Cumulative Percentage',
        overlaying='y',
        side='right',
        rangemode='tozero',
        tickvals=[0, 25, 50, 75, 100],
        tickformat='%',
    )
)
 
fig.show()

In the above example, we first sort the DataFrame df by the Frequency column in descending order, creating df_sorted. Then, we calculate the cumulative percentage of the frequencies and add this as a new column to df_sorted.

Next, we initialize a new plotly figure (fig) and add two traces: a bar chart representing the frequencies of different categories, colored blue, and a line plot representing the cumulative percentage, colored red using fig.add_trace() method.

We then update the layout of the figure to include titles for both axes and set the right y-axis for the cumulative percentage. Finally, we display the figure with fig.show().

We have now created a pareto chart showing the distribution of frequencies among different categories along with their cumulative percentages.
 

Video, Further Resources & Summary

Do you need more explanations on how to create a plotly pareto chart in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to create a plotly pareto chart in Python.

 

The YouTube video will be added soon.

 

So, we have demonstrated in this tutorial how to create an interactive plotly pareto chart in Python. Furthermore, you could have a look at some of the other interesting plotly in Python tutorials on Statistics Globe, starting with these:

This post has shown how to create a plotly pareto chart in Python. I hope you found it helpful! In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top