WeirdGeek

Data Science | Machine Learning | Automation

  • Data Analytics
  • Python
  • Data Science
  • Google Apps Script
  • Machine Learning
  • Artificial Intelligence
  • SQL Server

12/05/2020 By WeirdGeek Leave a Comment

Plot 100 percent stacked column chart using plotly in Python

We have seen in our previous posts how we can create 100 % stacked column chart in Tableau. Now in this post we will see how we can create 100 percent stacked column chart using plotly in Python.

Plotly is one of the best library for creating the interactive visualization charts. Its pretty easy to host it to any cloud platform to share it with other people. Now lets easy it is to create the chart using plotly in Python.

Code to plot the chart :

Lets first import the libraries which we need to plot the charts, which basically are:

  • Plotly for data visualization
  • Pandas for data read and data manipulation
import plotly.express as px
import pandas as pd
import plotly.graph_objs as go

Now import the data using the pandas libraries and check the column names as we need them while plotting the chart.

data= pd.read_csv(r"C:\Users\WeirdGeek\Desktop\Students.csv")
data.columns

So, now lets try to print the top five columns to get an idea about the above data set:

data.head()

100 % stacked column chartTo bring the data in the above format we use Pandas library and its data manipulation power. Let’s see each steps one by one.

  • First we bring the rows into columns by using pandas pivot_table function.
  • Then, we summed up the total marks in one column
  • Then finally we created new column for each subject for calculating the percentage of each subject. i.e. marks in one subject divided by total marks obtained.

Now let’s use the above formatted data into plotly library to plot the chart.

fig = go.Figure()
fig.add_trace(go.Bar(
    y=data["Maths %"],
    x=data.Name,
    name="Maths %",
    marker=dict(
        color='rgba(0,128,0, 0.6)',
        line=dict(color='rgba(0,128,0, 0.5)', width=0.05)
    )
))
fig.add_trace(go.Bar(
    y=data["Social Science%"],
    x=data.Name,
    name="Social Science %",
    marker=dict(
        color='rgba(0,0,255, 0.6)',
        line=dict(color='rgba(0,0,255, 0.5)', width=0.05)
    )
))
fig.add_trace(go.Bar(
    y=data["Economics %"],
    x=data.Name,
    name="Economics %",
    marker=dict(
        color='rgba(128,0,0, 0.5)',
        line=dict(color='rgba(128,0,0, 0.5)', width=0.05)
    )
))
fig.update_layout(
        yaxis=dict(
        title_text="Marks %",
        ticktext=["0%", "20%", "40%", "60%","80%","100%"],
        tickvals=[0, 20, 40, 60, 80, 100],
        tickmode="array",
        titlefont=dict(size=15),
    ),
    autosize=False,
    width=1000,
    height=400,
    paper_bgcolor='rgba(0,0,0,0)',
    plot_bgcolor='rgba(0,0,0,0)',
    title={
        'text': "Students Marks %",
        'y':0.96,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'},
    barmode='stack')
fig.show()

This is how the final chart looks like:

stacked column chart using plotly

In the above code we have used the generic function go.Bar from plotly.graph_objects. Then added the x and y data to the respective place and choose the color (RGB code) along with the width. then in update_layout() function, we add few parameters like, chart size, Title and its x and y coordinates, and finally the barmode which is the “stack” as we are here plotting the stacked bar chart. Then, finally final line of code to show the figure.

To get more detailed knowledge about charts in Plotly you can check the official website Plotly by Dash. Also if you want to explore more codes/posts related to Data visualization on our website, then you can find it here.

If you have any questions or if you need any help please get in touch with me using the comment section below.

Related posts:

  1. Plot stacked bar chart using plotly in Python
  2. Plot multiple bar graph using Python’s Plotly library
  3. Plotting 100 % stacked column chart in Tableau
  4. Plotting vertical bar graph using Plotly using Python

Filed Under: Data Analytics Tagged With: Data Visualization, Plotly, Python

Leave a Reply Cancel reply

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

Subscribe to my Blog !!

Enter your email below to subscribe my blog and get the latest post right in your inbox.

  • Home
  • Terms
  • Privacy
  • Contact Us

Copyright © 2025 · WeirdGeek · All trademarks mentioned are the property of their respective owners.