Plot excel file in python

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.

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.

Plot excel file in 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()

Plot excel file in python
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!

This tutorial is the one in which you will learn a basic method required for data science. That skill is to plot the data from an excel file in matplotlib in Python. Here you will learn to plot data as a graph in the excel file using matplotlib and pandas in Python.

How to plot data from excel file using matplotlib?

Before we plot the data from excel file in matplotlib, first we have to take care of a few things.

  • You must have these packages installed in your IDE:- matplotlib, pandas, xlrd.
  • Save an Excel file on your computer in any easily accessible location.
  • If you are doing this coding in command prompt or shell, then make sure your directories and packages are correctly managed.
  • For the sake of simplicity, we will be doing this using any IDE available.

Step 1: Import the pandas and matplotlib libraries.

import pandas as pd
import matplotlib.pyplot as plt

Step 2 : read the excel file using pd.read_excel( ‘ file location ‘) .

var = pd.read_excel('C:\\user\\name\\documents\\officefiles.xlsx')
var.head()

To let the interpreter know that the following \ is to be ignored as the escape sequence, we use two \.
the “var” is the data frame name. To display the first five rows in pandas we use the data frame .head() function.

If you have multiple sheets, then to focus on one sheet we will mention that after reading the file location.

var = pd.read_excel('C:\\user\\name\\documents\\officefiles.xlsx','Sheet1')
var.head()

Step 3: To select a given column or row.
Here you can select a specific range of rows and columns that you want to be displayed. Just by making a new list and mentioning the columns name.

varNew = var[['column1','column2','column3']]
varNew.head()

To select data to be displayed after specific rows, use ‘skip rows’ attribute.

var = pd.read_excel('C:\\user\\name\\documents\\officefiles.xlsx', skiprows=6)

step 4: To plot the graph of the selected files column.
to plot the data just add plt.plot(varNew[‘column name’]) . Use plt.show() to plot the graph.

import matplotlib.pyplot as plt
import pandas as pd

var= pd.read_excel('C:\\Users\\name\\Documents\\officefiles.xlsx')
plt.plot(var['column name'])
var.head()

plt.show()

You will get the Graph of the data as the output.

You may also love to learn:

  • Reading an excel sheet using “xlrd” module in Python 3.x or earlier
  • How to copy data from one excel sheet to another using python

How do I plot an Excel file in Python?

Plot data from Excel Sheet using Python.
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. ... .
Step 2: Loading Dataset. ... .
Step 3: Separating x and y values. ... .
Step 4: Plotting a Scatter Plot..

How do I plot a bar graph in Excel using Python?

For plotting the charts on an excel sheet, firstly, create chart object of specific chart type( i.e Bar, Stacked Bar, Percent Stacked Bar chart etc.). After creating chart objects, insert data in it and lastly, add that chart object in the sheet object. Code #1 : Plot the simple Bar Chart.

How do you plot data in Python?

Following steps were followed:.
Define the x-axis and corresponding y-axis values as lists..
Plot them on canvas using . plot() function..
Give a name to x-axis and y-axis using . xlabel() and . ylabel() functions..
Give a title to your plot using . title() function..
Finally, to view your plot, we use . show() function..

How do you plot values of data in Excel?

How to Make a Graph in Excel.
Enter your data into Excel..
Choose one of nine graph and chart options to make..
Highlight your data and click 'Insert' your desired graph..
Switch the data on each axis, if necessary..
Adjust your data's layout and colors..
Change the size of your chart's legend and axis labels..