Visualizing Excel Data in Python: A Beginner’s Guide to Creating Pie Charts with Matplotlib

Somanathan Gohulan
4 min readMar 22, 2023

Visualizing data is an essential part of data analysis and a great way to gain insights from your data. Python offers a variety of libraries for data visualization, with Matplotlib being one of the most popular ones. In this beginner’s guide, we will explore how to use Matplotlib to create pie charts from Excel data. Pie charts are an effective way to represent data in a clear and concise manner, making them ideal for visualizing proportions and percentages. By the end of this guide, you will have learned how to create your own pie charts from Excel data using Python and Matplotlib. So, let’s get started!

Complete code :- https://github.com/Gohulan/ExcelPie

Project Setup

  1. Open Visual Studio and create a new Python project by selecting “File” > “New” > “Project”.
  2. Select “Python” > “Python Application” as the project type and choose a name and location for your project.
  3. Once the project is created, open the “Solution Explorer” and right-click on your project name, then select “Manage Python Packages”.
  4. Search for and install the following packages: pandas, matplotlib, openpyxl.
  5. Create a new Python file by right-clicking on your project name and selecting “Add” > “New Item” > “Python File”. Name the file something like “excel_piechart.py”.
  6. In the “excel_piechart.py” file, import the required packages and write the following code to read the Excel file and create a pie chart:

Excel File

Before starting this project please create a excel file with the name of Sales.xlsx and Worksheet name :- Sales_Data, add the data as given below

Load Excel data into Python

Once you have Pandas installed, you can use it to read in Excel data like this:

In this code, openpyxl import load_workbook is used to read in the Excel data from the specified file path and create a pandas dataframe (df) containing the data. You can then use the Pandas library to manipulate and analyze the data as needed.

Let’s Start Visualizing Data

Data visualization is an essential part of any data analysis project. It is the process of representing data in a visual form such as charts, graphs, and plots to help identify patterns and trends in the data. In this tutorial, we will be using Matplotlib, a popular Python library, to create a pie chart from Excel data.

To get started, we will first need to load our Excel data into Python using the Pandas library. Once we have our data in a Pandas dataframe, we can use Matplotlib to create our pie chart.

First, we need to select the worksheet from the Excel file. In our case, the worksheet is called “Sales_Data”.

Next, we will read the data into a Pandas dataframe. We can use the values property of the worksheet to get the values as a 2D array and then create a dataframe from it.

The first row of the dataframe will contain our column names. We can set the column names of the dataframe to the values of the first row.

We can drop the first row (column names) and any rows with NaN (Not a Number) values using the dropna() method.

Now, we can calculate the total amount for each sale item by multiplying the quantity with the amount and adding it as a new column to the dataframe.

To create a pie chart using Matplotlib, we first need to create a figure. We can use the figure() function to create a new figure and set the title of the figure using the suptitle() function.

We can then use the pie() function to create a pie chart. We pass the values of the "Total Amount" column as the first argument, the labels of the "Sale Item" column as the second argument, and the format of the percentage as the autopct argument.

Finally, we can set the title of the pie chart using the title() function and display the chart using the show() function.

Happy Visualizing !!

--

--

Somanathan Gohulan

Happiest person,Too much of Interest in Technology, Programmer, Positive attitude n all walks of life