All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Plot Graph In Python Using CSV File

Last Updated : Jul 1, 2023

How To Plot Graph In Python Using CSV File

In this tutorial we will show you the solution of how to plot graph in python using csv file, CSV stands for Comma Separated Values, a popular format to store structured data.

A CSV file contains a number of records, with the data spread across rows and columns. We often need to visualize the data stored in a CSV file.

When you are working as a data scientist, you often have to analyze the data in Python. Let’s get started.

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

Python provides different kinds of plots for data visualization. You can use the Matplotlib.pyplot library to perform this kind of operation.

Matplotlib is an open-source and popular data visualization library in Python.

It has a sub-module called pyplot, which is used to plot graphs in Python.

To use matplotlib, we must install it first using the following command - pip install matplotlib

import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('Student.csv')
name=data['StudentName']
marks=data['StudentMarks']
x=list(name)
y=list(marks)
plt.pie(y, labels=name, autopct='%.2f%%')
plt.xlabel('Names')
plt.ylabel('Marks')
plt.title('Marks of different students')
plt.legend()
plt.show()
  1. In this article we will plot the pie graph using a .CSV file named student.csv, which has some details of students and their marks.
  2. The student.csv file contains three columns named “StudentName”, “StudentMarks”, and “StudentDepartment”.
  3. Now in the first line there is an import of the matplotlib.pyplot library, which is necessary to include in this program.
  4. In the next line there is another import statement which imports the pandas module as “pd”, which will help to achieve the target.
  5. In the next line, the pd.read_csv() function will read the file, and there is a variable called data, which will store the data from the file student.csv.
  6. Now there are two variables in the next two lines: The first one is the variable name that will store the values from the “StudentName” column.
  7. And the second one is the marks variable, which will store the values from the “StudentMarks” column.
  8. In the next two lines, there are another two variables named x and y. The list function will return a list created from the values of variables “name” and “marks”.
  9. The x variable will store the list returned from the values of variable “name” and the y variable will store the list returned from the values of variable “marks”.
  10. In the next line, there is a pie() function that will create the pie graph. We have passed the list of marks and student names as labels on the graph. We have also displayed the percentage of each portion using the autopct.
  11. In the next two lines, we set the labels for the pie graph. The xlabel() function sets the label on the x-axis and the ylabel() function on the y-axis. In this case, it will be “Name” and “Marks” respectively.
  12. The title() function will display the title of the graph. The legend() function will display an area describing the elements of the graph.
  13. The show() function will display the graph as the output.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now plot a graph using any .csv file in a Python program you can use any graph to plot the data from any .csv file.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪