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

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

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

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

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.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.