AdventureWorks DW Series 3 : How to Create Pie Chart Visualization Using Python

In this blog we create a visualization in Python over AdventureWorksDW to showcase the total Sales amount across different countries. The visualization will contain the pie chart to showcase the sales for different region or countries.

In the data model of AdventureWorksDW the fact table FactInternetSales has the` transactions where in we find the sales amount incurred in each order transaction.

The Data Model to fetch the required data is as follows:

The join between FactInternetSales and DimSalesTerritory can be made using field SalesTerritoryKey.

So, the first step is to write a sql query that can fetch the sum of SalesAmount based on SalesTerritoryCountry. The SalesTerritoryCountry column is in the table DimSalesTerritory. So, we can join the above two tables based on join condition mentioned above and using the sql function sum with group by clause based on column SalesTerritoryCountry to get the Total Sales amount country wise. The below is the sql query for the same :

Now we will write a python code to connect to AdwentureWorksDW database stored in Microsoft SQL server. To learn how to connect python to Microsoft SQL Server please refer to below blog:

So, the python solution for the above problem would begin with making ODBC connection from python to Microsoft SQL server by using the library pyodbc. After connection is established the python code would execute the above query and fetch the results in a python data structure. The code piece for the above solution is as follows:

So, once we have query executed, now the next is to fetch the data. If we are running the same query from Microsoft SQL server the output will looks like as follows:

This is relevant when we process data in python as sequence of fields or columns in each row is important.

To make the visualization the programmer must be familiar with the basic functionality of matplotlib library in python for which the below forum can be referred:

Now to plot the data we would need three lists i.e. one for storing the country names, one for storing total sales amount in the respective country and one for stroring name of color to represent each country’s sale in pie chart. Below is the code:

There is another variable used here is explore. The length of this variable is 6 which is equivalent to number of countries we have i.e. 6 as we can see in sql query output above. The first 5 values are zero and last one is 0.1. The value signifies that in the pei chart the sixth visual slice will be exploded which corresponds to country United States.

Now we have all the required details to go for plotting. Below is the code to draw the pie chart from the required data :

The output of the above code is pie chart based visualization. The visualization we will get after code execution is as follows:





There are no entries yet.
Please enter the code
* Required fields

Subscribe to Our YouTube Channel To recieve updates on Free Learning



Visit Our Website to View the Upcoming Training Schedule

Looking for Corporate Training ? Reach out to us at

Reach out to us if you are looking for Corporate Training to Build The Next Generation Analytical Workforce with an in-depth understanding of  Exploratory Data Analysis , Data Visualisation, Data Analytics , AI First , Machine Learning & Deep Learning Training & Consulting helping them to take Data Informed Decision at each stage of the business. 

We understand that At the present times , the Entire Industry is in a Tranformation stage with the Softwares  being rebuilt with Artificial Intelligence Capabilities .

We need SMART WORKFORCE for the SMART SOFTWARES to reap the maximum return . 


Whatsapp at +91-9953805788 or email at - if you would like to know more . 


  • Corporate Tableau Training in Gurgaon
  • Corporate Data Analytics Training in Gurgaon
  • Corporate Microsoft Power BI Training in Gurgaon
  • Corporate Microstrategy Training in Gurgaon
  • Corporate Google Data Studio Training in Gurgaon
  • Corporate Python Training in Gurgaon
  • Corporate Advance Analytics in R Programming Training in Gurgaon
  • Corporate Machine Learning Training in Gurgaon
  • Corporate Deep Learning Training in Gurgaon
  • Corporate Data Visualization Training in Gurgaon



Instrovate Technologies

Noida, Gurgaon 

Hit Your Refresh Button To Rise Higher

Subscribe to Our YouTube Channel To Receive Updates on the Latest Free Learning Video Added

Print Print | Sitemap
All rights reserved @ Instrovate Technologies

Terms Of Services    Privacy Policy    Disclaimer     Refund Policy