How to plot Bar Graph in Python using CSV file? Last Updated : 25 Feb, 2021 Comments Improve Suggest changes Like Article Like Report CSV stands for "comma separated values", that means the values are distinguished by putting commas and newline characters. A CSV file provides a table like format that can be read by almost every spreadsheet reader like Microsoft Excel and Google Spreadsheet. A Bar Graph uses labels and values where label is the name of a particular bar and value represent the height of the bar. A Bar Graph is commonly used in data analytics where we want to compare the data and extract the most common or highest groups. In this post, we will learn how to plot a bar graph using a CSV file. There are plenty of modules available to read a .csv file like csv, pandas, etc. But in this post we will manually read the .csv file to get an idea of how things work. Functions UsedPandas read_csv() function is used to read a csv file. Syntax: read_csv("file path") Matplotlib's bar() function is used to create a bar graph Syntax: plt.bar(x, height, width, bottom, align) Method 1: Using pandas Approach Import moduleRead file using read_csv() functionPlot bar graphDisplay graph Example: Dataset in use: Click here Python3 # Import the necessary modules import matplotlib.pyplot as plt import pandas as pd # Initialize the lists for X and Y data = pd.read_csv('C:\\Users\\Vanshi\\Desktop\\data.csv') df = pd.DataFrame(data) X = list(df.iloc[:, 0]) Y = list(df.iloc[:, 1]) # Plot the data using bar() method plt.bar(X, Y, color='g') plt.title("Students over 11 Years") plt.xlabel("Years") plt.ylabel("Number of Students") # Show the plot plt.show() Output: Method 2: Using Matplotlib Approach Import moduleOpen fileRead dataPlot bar graphDisplay graph Program: Dataset in use: Click here to download this file. Python3 # Import the modules import matplotlib.pyplot as plt # Initialize a dictionary for months data = dict() # Read the data with open('electronics.csv', 'r') as f: for line in f.readlines(): # Store each line in the dictionary month, item, quantity = line.split(',') if month not in data: data[month] = [] data[month].append((item, int(quantity))) # Position of each subplot where 221 means 2 row, # 2 columns, 1st index positions = [221, 222, 223, 224] # Colors to distinguish the plot colors = ['r', 'g', 'b', 'y'] # Plot the subgraphs for i, l in enumerate(data.keys()): plt.subplot(positions[i]) data_i = dict(data[l]) plt.bar(data_i.keys(), data_i.values(), color=colors[i]) plt.xlabel(l) # Show the plots plt.show() Output: Comment More infoAdvertise with us Next Article How to plot Bar Graph in Python using CSV file? M mukulbindal170299 Follow Improve Article Tags : Python Python-matplotlib Data Visualization Practice Tags : python Similar Reads How to plot a graph in R using CSV file ? To plot a graph in R using a CSV file, we need a CSV file with two-column, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be looking at the way to plot a graph 2 min read Bar chart using Plotly in Python Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization librar 4 min read How to create a bar chart and save in pptx using Python? World Wide Web holds large amounts of data available that is consistently growing both in quantity and to a fine form. Python API allows us to collect data/information of interest from the World Wide Web. API is a very useful tool for data scientists, web developers, and even any casual person who w 10 min read How to plot data from a text file using Matplotlib? Perquisites: Matplotlib, NumPy In this article, we will see how to load data files for Matplotlib. Matplotlib is a 2D Python library used for Date Visualization. We can plot different types of graphs using the same data like: Bar GraphLine GraphScatter GraphHistogram Graph and many. In this article, 3 min read Reading Rows from a CSV File in Python CSV stands for Comma Separated Values. This file format is a delimited text file that uses a comma as a delimiter to separate the text present in it. Or in other words, CSV files are used to store data in tabular form. As per the name suggested, this file contains the data which is separated by a co 5 min read Histogram using Plotly in Python Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library 3 min read How to Create a Population Pyramid Using Plotly in Python? A population pyramid is a graphical representation of data that contains two entities, namely the age and gender of a specific population. It is generally used by demographers to study the population. The age value is divided into sub-categories and the gender column contains the population of that 3 min read Plotting multiple bar charts using Matplotlib in Python Matplotlib is a powerful visualization library in Python that allows for the creation of various types of plots, including bar charts. When working with multiple bar charts, we can represent data in two main ways, grouped bar charts (multiple bars within one chart) and separate bar charts (multiple 3 min read Circular Bar Plot in Python In this guide, we'll dive into the world of circular bar plots in Python. We'll explore what they are, why they're useful, and how you can create them to add a stylish touch to your data visualization projects. What is a Circular Bar Plot?Circular bar plots, also known as radial bar charts or circul 4 min read How to Change the Color of a Graph Plot in Matplotlib with Python? Prerequisite: Matplotlib Python offers a wide range of libraries for plotting graphs and Matplotlib is one of them. Matplotlib is simple and easy to use a library that is used to create quality graphs. The pyplot library of matplotlib comprises commands and methods that makes matplotlib work like ma 2 min read Like