Matplotlib Pie Chart Examples

1 Create a pie chart that resembles a Trivial Pursuit Mover:

  • The mover has 6 wedges of equal size.
  • The colors of the mover are red, yellow, green, blue, pink, and brown.
  • Add shadow for a nicer look.
  • Make 2 of the wedges placed next to one another explode (stand out).
import matplotlib.pyplot as plt

data = [1,1,1,1,1,1]
colors = ['red', 'yellow', 'green', 'blue', 'pink', 'brown']

plt.pie(data, colors=colors, shadow=True, explode=[0.1,0,0.1,0,0,0])
plt.show()

Output

Trivial Pursuit Mover Matplotlib Pie Chart

2 According to the University of Waterloo, world energy consumption in 2006 from the five top energy resources was:

Natural Gas - 24%
Hydro - 6%
Nuclear 6%
Oil - 36%
Coal - 28%

Build a pie chart of the distribution of world energy consumption based on the data above.

import matplotlib.pyplot as plt

data = [24, 6, 6, 36, 28]
label = ['Natural Gas', 'Hydro', 'Nuclear', 'Oil', 'Coal']

plt.pie(data, labels=label, autopct='%1.1f%%', explode=[0,0,0,0.1,0], shadow=True, startangle=90)
plt.title('World Energy Consumption')
plt.axis('equal')
plt.show()

Output

World Energy Consumption Matplotlib Pie Chart

3 Create a pie chart in Matplotlib using the following data:

dataQ3=[200,100,80,160,120]
labelQ3=['numpy','pandas','matplotlib','scipy','pillow']
  1. Set explode parameter to emphasize 'matplotlib' slice.
  2. Set color for every slice to search color picker.
  3. Set title='Python packages and modules'
import matplotlib.pyplot as plt

dataQ3=[200,100,80,160,120]
labelQ3=['numpy','pandas','matplotlib','scipy','pillow']

plt.pie(dataQ3, labels=labelQ3, explode=(0,0,0.1,0,0), autopct='%1.1f%%')
plt.title('Python packages and modules')
plt.axis('equal')
plt.show()

Output

Matplotlib Pie Chart Example

4 Create a pie chart that shows the percentage of employees in each department within a company. The provided file: employee_count_by_department.txt contains the data required in order to generate this pie chart.

employee_count_by_department.txt
Department Name, Total number of employees
Marketing, 50
Information Technology, 275
Management, 230
Human Resources, 250
Finance, 92
Supply Chain, 73
Manufacturing, 30
import csv
import matplotlib.pyplot as plt

filename = 'employee_count_by_department.txt'
#creating header and rows list
header = []
data = []
labels = []
#reading csv file
with open(filename, 'r') as employeefile:
  #created a stock reader object
  employeereader = csv.reader(employeefile)

  #extracting the field names from the first row
  header = employeereader.__next__()

  #extracting data from rows one by one by iterating through amznreader
  for row in employeereader:
    data.append(row[1])
    labels.append(row[0])

plt.pie(data, labels=labels, autopct='%1.1f%%')
plt.axis('equal')
plt.show()

Output

Employee Count by Department Matplotlib Pie Chart

5 Create a text file that contains your expenses for last month in the following categories: The file should be called "expenses.txt".

  • Rent
  • Gas
  • Food
  • Clothing
  • Car Payment
  • Misc

Write a Python program that reads the expense file data and shows the total monthly expenses figure (Dollar sign, commas, two decimal places). Use the "matplotlib" library to display the data as a pie chart.

The chart should be appropriately titled and labeled as "Expenses".

import csv
import matplotlib.pyplot as plt

filename = 'expenses.txt'
#creating header and rows list
header = []
data = []
labels = []
#reading csv file
with open(filename, 'r') as expensesfile:
  #created a stock reader object
  expensereader = csv.reader(expensesfile)

  #extracting the field names from the first row
  header = expensereader.__next__()
  
  #extracting data from rows one by one by iterating through amznreader
  for row in expensereader:
    #rows.append(row)
    data.append(row[1])
    labels.append(row[0])

plt.pie(data, labels=labels, autopct='%1.1f%%')
plt.title('Expenses')
plt.axis('equal')
plt.show()

Output

Expenses Matplotlib Pie Chart

6 Write a Python program that uses matplotlib to plot the sales values as a pie chart. When plotting, specify the color of each pie slice using Purdue's secondary color palette shown in Table 1. In addition, the pie chart should include a title, and every slice in the chart should be labeled with the name of the month it represents. Test your program with the data in Table 2.

Color Name Hex Code
CoalGray 4D4038
MoonDustGray BAA892
EverTrueBlue 5B6870
SlayterSkyBlue 6E99B4
AmeliaSkyBlue A3D6D7
LandGrantGreen 085C11
RossAdeGreen 849E24
CeleryBogGreen C3BE0B
SpringFestGreen E9E45B
OakenBucketBrown 6B4536
BellTowerBrick B46012
MackeyOrange FF9B1A

Table 1: Purdue Secondary Colors

Month Sales
January 2000
February 4000
March 3000
April 5000
May 6000
June 4000
July 2000
August 5000
September 8000
October 5000
November 6000
December 10000

Table 2: Test Sales Data

import matplotlib.pyplot as plt

months = ['January','February','March','April','May','June','July','August','September','October','November','December']
sales = [2000,4000,3000,5000,6000,4000,2000,5000,8000,5000,6000,10000]
colors = ['#4D4038','#BAA892','#5B6870','#6E99B4','#A3D6D7','#085C11','#849E24','#C3BE0B','#E9E45B','#6B4536','#B46012','#FF9B1A']

plt.pie(sales, labels=months, colors=colors, shadow=True)
plt.title('Monthly Sales')
plt.axis('equal')
plt.show()

Output

Monthly Sales Matplotlib Pie Chart

7 Write a Python code that would display a pie chart of the popularity of programming languages.

  1. x_axis: Programming languages (Java, Python, PHP, JavaScript, C#, C++)
  2. y_axis: Popularity (22.2, 17.6, 8.8, 8, 7.7, 6.7)
  3. Label the portions of the pie the following.
  4. Display portion value on the pie.
  5. Shadow the pie.
  6. Start the pie from an angle of 140°.
import matplotlib.pyplot as plt

x_axis = ['Java','Python','PHP','JavaScript','C#','C++']
y_axis = [22.2,17.6,8.8,8,7.7,6.7]

plt.pie(y_axis, labels=x_axis, shadow=True, startangle=140, autopct='%1.1f%%', explode=[0.1,0,0,0,0,0])
plt.axis('equal')
plt.show()

Output

Programming Language Matplotlib Pie Chart

Recommended Posts