1 Create a pie chart that resembles a Trivial Pursuit Mover:
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
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']
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' #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
5 Create a text file that contains your expenses for last month in the following categories: The file should be called "expenses.txt".
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
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
7 Write a Python code that would display a pie chart of the popularity of programming languages.
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