All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Plot Bar Graph In Python Using CSV File

Last Updated : Mar 11, 2024

How To Plot Bar Graph In Python Using CSV File

In this article we will show you the solution of how to plot bar graph in python using csv file, in simple terms bar graph means bar chart, it is used for representation of data.

And CSV file means a (comma- separated values) file is a text file that has a special arrangement which allows data to be saved in a table structured arrangement.

Step By Step Guide On How To Plot Bar Graph In Python Using CSV File :-

A bar graph is a map that plots data using blockish bars or columns (called boxes) that represent the total volume of observances in the data for that order.

Bar maps can be displayed with perpendicular columns, vertical bars, relative bars (multiple bars to display a comparison between values), or piled bars (bars holding many varieties of data).

A CSV File (comma- separated values) is a text file that stores data in the configuration of columns, separated by commas, and rows are separated by line breaks.

Comma separated value file means that the data is truly intake as data that's separated by commas.

The spreadsheet usage converts those comma- separated bits of data into cells in tables and columns to produce it simple to read and edit.

There are plenty of modules accessible to read a. csv file like csv, pandas, etc.

Following are the steps for plotting the bar graph in python using csv file.

  • Import module
  • Read file using read_csv() function
  • Plot bar graph
  • Display graph
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('data.csv')
df = pd.DataFrame(data)
X = list(df.iloc[:, 0])
Y = list(df.iloc[:, 1])
plt.bar(X, Y, color='g')
plt.title("innovative companies")
plt.xlabel("Countries")
plt.ylabel("Number of Companies")
plt.show()
  1. In the first two lines of code, we import modules. Matplotlib for creating bar graph and pandas for reading csv file.
  2. After that we use csv file for reading data.
  3. Then we create the dataframe.
  4. Then we initialize the lists for X and Y.
  5. We use .iloc which is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.
  6. Then we plot the bar graph using bar method
  7. Then we display the graph by using show method.

Conclusion :-

Hence, we have successfully learnt how plot the bar graph with the help of csv file in python.

I hope this article on how to plot bar graph in python using csv file helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪