How to generate BAR plot using pandas DataFrame?

This recipe helps you generate BAR plot using pandas DataFrame

Recipe Objective

visualizing a dataset give us a overall view of the data. It gives various statistical description about the data.

So this is the recipe on how we can generate BAR plot using pandas DataFrame. .

Step 1 - Importing Library

import pandas as pd import matplotlib.pyplot as plt import numpy as np

We have only imported pandas, numpy and matplotlib.pyplot which is needed.

Step 2 - Creating DataFrame

We have created a Dictionary and passed it through pd.DataFrame to create dataframe with different features. raw_data = {"first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"], "pre_score": [4, 24, 31, 2, 3], "mid_score": [25, 94, 57, 62, 70], "post_score": [5, 43, 23, 23, 51]} df = pd.DataFrame(raw_data, columns = ["first_name", "pre_score", "mid_score", "post_score"]) print(df)

Step 3 - Creating Bar Plot

We have done various steps to plot bar graph. First we have assigned labels to the bar, then the y and horizontal position of the the bar. Bar graph is ploted by the function plt.barh and finally labelling the x, y axis and the graph. Molly = df.ix[1, 1:] Tina = df.ix[2, 1:] bar_labels = ["Pre Score", "Mid Score", "Post Score"] plt.figure(figsize=(8,6)) y_pos = np.arange(len(Molly)) y_pos = [x for x in y_pos] plt.yticks(y_pos, bar_labels, fontsize=10) plt.barh(y_pos, Molly, align="center", alpha=0.4, color="#263F13") plt.barh(y_pos, -Tina, align="center", alpha=0.4, color="#77A61D") plt.xlabel("Tina"s Score: Light Green. Molly"s Score: Dark Green") plt.title("Comparison of Molly and Tina"s Score") plt.ylim([-1,len(Molly)+0.1]) plt.xlim([-max(Tina)-10, max(Tina)+10]) plt.grid(); plt.show() So the output comes as


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

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

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.

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.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch