WeirdGeek

Data Science | Machine Learning | Automation

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

10/05/2020 By WeirdGeek 1 Comment

Plot stacked bar chart using plotly in Python

Here in this post we will plot stacked bar chart using plotly in Python. As everyone say that data is the new oil, but on top of that i can say that data visualization is the tool which adds importance to that oil and without that oil aka data is of no use.

Lets plot the tacked bar chart now.

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 lets read the data and print the data

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

Here’s how our data looks like:

 stacked bar chart

Now let’s see the Plotly code to plot stacked bar chart:

fig = px.bar(data,x='Name ', y='Marks', color='Subject',height=400, width=700)
fig.show()

The above code will take the name column on the x axis and marks column on the y axis and to add the color which finally prepare the stacked bar chart we add the Subject to the color attribute. Also, we provided the height and width of the chart.

Here’s how the charts looks like:

Stacked bar chart using plotly

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 multiple bar graph using Python’s Plotly library
  2. Plotting vertical bar graph using Plotly using Python
  3. Plotting 100 % stacked column chart in Tableau
  4. Plotting horizontal bar graph using Plotly using Python

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

Comments

  1. Patryk says

    28/04/2022 at 5:30 am

    Why did you import plotly.graph_objs as go as you did not use that in the code?

    Reply

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 © 2023 · WeirdGeek · All trademarks mentioned are the property of their respective owners.