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

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

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.