How to generate grouped BAR plot in Python?

This recipe helps you generate grouped BAR plot in Python

Recipe Objective

It is very easy to understand the data if we have visual representation of data. Visual representation of data can be done in many formats like histograms, pie chart, bar graphs etc

This python source code does the following:
1. Creates and converts data dictionary into dataframe
2. Groups different bar graphs
3. Plots the bar graphs by adjusting the position of bars

In this code recipe we will learn how to plot bar graphs for different class of data.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

Step 1 - Import the library

import pandas as pd import matplotlib.pyplot as plt

We have imported pandas and plt from matplotlib.pyplot library.

Step 2 - Setup the Data

Here we have created a dictionary named exam_data and passed that in pd.DataFrame to create a DataFrame with columns 'name', 'Maths_score', 'Science_score' and 'French_score'. We have also used a print statement to print the dataframe. exam_data = {'name': ['Aman', 'Alen', 'Mona', 'Jhon', 'Sheldon'], 'Maths_score': [44, 24, 31, 10, 16], 'Science_score': [25, 44, 7, 62, 75], 'French_score': [5, 13, 63, 53, 40]} df = pd.DataFrame(exam_data, columns = ['name', 'Maths_score', 'Science_score', 'French_score']) print(df)

Step 3 - Setting Position and Width of the bars in Graph

There have various strings in the column 'names' and for all of that we have to plot the bars. So for that we have to set the position of the bars. We are defining objects for position and width of bars and we are calling subplots with size of the figure as a parameter. pos = list(range(len(df['Maths_score']))) width = 0.25 fig, ax = plt.subplots(figsize=(10,5))

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

Step 5 - Creating bars for the data

Now we have to plot the bars for the data by plt.bar. Let us have a look on the parameters first:

  • x : In this parameter we have to pass the values for which we want to plot bars.
  • width : It this parameter we have to set the width of the bars. By default it is 0.8
  • aplha : In this parameter we have to pass values between 0 and 1 and it is a parameter to set the transparency of the bars.
  • color : It this parameter we have to pass the colour code in which we want to have the bars.
  • pos : In this parameter we have to set the position of the bars.
  • align : It this parameter we have to set the alignment of the bar by center or edge. By default it is center.

plt.bar(pos, df['Maths_score'], width, alpha=0.5, color='#EE3224') plt.bar([p + width for p in pos], df['Science_score'], width, alpha=0.5, color='#F78F1E') plt.bar([p + width*2 for p in pos], df['French_score'], width, alpha=0.5, color='#FFC222') Now have to set the other elements of the graph like x and y axis label, title of graph, setting x and y axis limits and the legend. These all can be done by the functiond below with the comments given. After that we can set the grid by plt.grid() and we can view the graph by plt.show(). # Setting the y axis label ax.set_ylabel('Score') # Setting the chart's title ax.set_title('Test Subject Scores') # Setting the position of the x ticks ax.set_xticks([p + 1.5 * width for p in pos]) # Setting the labels for the x ticks ax.set_xticklabels(df['name']) # Setting the x-axis and y-axis limits plt.xlim(min(pos)-width, max(pos)+width*4) plt.ylim([0, max(df['Maths_score'] + df['Science_score'] + df['French_score'])] ) # Adding the legend and showing the plot plt.legend(['Maths Score', 'Science Score', 'French Score'], loc='upper left') plt.grid() plt.show() So the output comes as

      name  Maths_score  Science_score  French_score
0     Aman           44             25             5
1     Alen           24             44            13
2     Mona           31              7            63
3     Jhon           10             62            53
4  Sheldon           16             75            40

Download Materials

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.