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

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

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

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.