In: Computer Science
Graphs with Matplotlib Using the library Matplotlib and the provided data files create the following graphs:
I) Pie chart
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.
II) Line Graph
Create a line graph that shows a company's profit over the past ten years. The provided file: last_ten_year_net_profit.txt contains the data required in order to generate this line graph.
III) Bar Graph
Create a bar graph that shows a company's profit over the past ten years. The provided file: last_ten_year_net_profit.txt contains the data required in order to generate this bar graph. Info: Be sure to label all the axes of each graph with meaningful labels and provide an appropriate title to each of the graphs (mentioned above) being generated. Hint, the names of the files and the headers provided in each of the data files should help
-----------------------
last_ten_year_net_profit.txt
Year ; Profit in $USD
2009 ; $175,000
2010 ; $250,000
2011 ; $525,000
2012 ; $239,000
2013 ; $1,000,000
2014 ; $1,000,500
2015 ; $500,000
2016 ; $740,000
2017 ; $5,625,000
2018 ; $100,000,000
--------------------------
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
# matplotlib library
import matplotlib.pyplot as plt
# bar graph
def barGraph(x_values,y_values):
bargraph = plt.figure(figsize=(6,4))
plt.bar(x_values,y_values)
plt.xlabel("year")
plt.ylabel("profit")
plt.title("yearly vise profits")
plt.show()
# pie chart
def pieChart(x_values,y_values):
piechart = plt.figure(figsize=(6,4))
plt.pie(y_values, labels =
x_values,autopct='%1.2f%%')
plt.title("percentage of employees department
vise")
plt.show()
# line graph
def lineGraph(x_values,y_values):
linegraph = plt.figure(figsize=(6,4))
plt.plot(x_values,y_values)
plt.xlabel("year")
plt.ylabel("profit")
plt.title("yearly vise profits")
plt.show()
# main function
def main():
x_values = []
y_values = []
# read input file
with open("last_ten_year_net_profit.txt","r") as
rdf:
lines = rdf.readlines()
# line by line
for line in lines[1:]:
# separate
columns by ;
tup =
line.split(";")
# type
conversion
x_values.append(int(tup[0]))
value=0
for i in
tup[1]:
try:
value=value*10+int(i)
except:
continue
y_values.append(value)
# plot bar graph
barGraph(x_values,y_values)
# plot line graph
lineGraph(x_values,y_values)
x_values=[]
y_values=[]
# read input file
with open("employee_count_by_department.txt","r") as
rdf:
lines = rdf.readlines()
# line by line
for line in lines[1:]:
# separate
columns by ,
tup =
line.split(",")
# type
conversion
x_values.append(tup[0])
y_values.append(int(tup[1]))
# plot pie chart
pieChart(x_values,y_values)
if __name__ == '__main__':
main()