Plot data from Excel Sheet using Python

Featured Img Excel2plot

In this tutorial, we will be learning a basic requirement in the world of Machine Learning and data science. Plotting the data can help to visualize the data and helps in a better understanding of the data points.

Steps to Plot data from Excel Sheet using Python

Today we will be making use of an excel sheet to plot data with the help of pandas and matplotlib modules in Python programming. So let’s begin!

Step 1: Importing Modules

We will be importing matplotlib and pandas modules in which the matplotlib module is used for plotting and pandas is used to handle the excel file datapoints.

Before importing the module, we need to make sure that the modules are installed in our system which is done using the following command on CMD ( Command Prompt ).

pip install pandas
pip install matplotlib

Now we will be importing both the modules in our program using the code lines below.

import pandas as pd
import matplotlib.pyplot as plt

Step 2: Loading Dataset

To load the data we will be using read_excel function from the pandas module which will take the path of the excel file as a parameter.

For this tutorial, we created a sample excel sheet containing some sample data points as shown in the image below.

Sample Excel Sheet Data Chosen Plot data from Excel Sheet using Python
Sample Excel Sheet Data Chosen

Now, the loading of data is done with the help of the code statement mentioned below.

var = pd.read_excel("sample_excel.xlsx")
print(var)

Step 3: Separating x and y values

Now in the dataset, we have two columns one for x data points and the other for y data points. This is done after separating the first and second columns into separate variables.

x = list(var['X values'])
y = list(var['Y values'])

After the separation of the x and y coordinates, we will be making a scatter plot for the data in the next step.

Step 4: Plotting a Scatter Plot

The scatter plot is displayed with the help of the code block mentioned below.

plt.figure(figsize=(10,10))
plt.style.use('seaborn')
plt.scatter(x,y,marker="*",s=100,edgecolors="black",c="yellow")
plt.title("Excel sheet to Scatter Plot")
plt.show()
Final Plot Excel Data
Final Plot Excel Data

You can try out a different dataset or search for a random dataset in excel format online. I hope you understood the concept thoroughly and will be able to implement the same yourself!

Thank you for reading! Happy Learning!