# 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.
• 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.show()```

Output 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 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 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'
data = []
labels = []
with open(filename, 'r') as employeefile:

#extracting the field names from the first row

#extracting data from rows one by one by iterating through amznreader
data.append(row)
labels.append(row)

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

Output 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'
data = []
labels = []
with open(filename, 'r') as expensesfile:

#extracting the field names from the first row

#extracting data from rows one by one by iterating through amznreader
#rows.append(row)
data.append(row)
labels.append(row)

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

Output 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
LandGrantGreen 085C11
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.title('Monthly Sales')
plt.axis('equal')
plt.show()```

Output 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.
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 