Let’s Create a Bar Graph in Python using CSV data.

Aswin Satheesh
featurepreneur
Published in
2 min readSep 3, 2021
Photo by Isaac Smith on Unsplash

What Is a Bar Graph?

A bar graph is a chart that plots data using rectangular bars or columns (called bins) that represent the total amount of observations in the data for that category. Bar charts can be displayed with vertical columns, horizontal bars, comparative bars (multiple bars to show a comparison between values), or stacked bars (bars containing multiple types of information).

What Is a CSV File?

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

Comma separated value file means that the data is actually input as data that is separated by commas. The spreadsheet application converts those comma-separated pieces of data into cells in tables and columns to make it easier to read and edit.

Now let’s start creating bar graph.

  1. First we have to Import necessary modules
import matplotlib.pyplot as pltimport pandas as pd

2. Next we have to read the csv file data.

data = pd.read_csv('data.csv')

here we are using a csv file named ‘data.csv’.

innovative companies list

3. Create DataFrame

df = pd.DataFrame(data)

4. Initialize the lists for X and Y

X = list(df.iloc[:, 0])Y = list(df.iloc[:, 1])

.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.

5. Plot bar graph

# Plot the data using bar() methodplt.bar(X, Y, color='g')plt.title("innovative companies")plt.xlabel("Countries")plt.ylabel("Number of Companies")

6. Display graph

# Show the plotplt.show()

Here is our final code !

code

Here is our Bar Graph!

Bar Graph

Successfully we’ve created our Bar Graph in Python using CSV data.

Thanks for reading. Hope it was useful.

Happy learning.

--

--