Plot data from excel file in matplotlib Python

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:

3 responses to “Plot data from excel file in matplotlib Python”

  1. hassan khan says:

    Thank you so much very helpful i tried for this finally solved my problem

  2. Shaikh says:

    So far so good. Now, how can I now export this plot to that Excel Sheet? Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *