Sem1 Module2 Unit1 Graphs
December 29, 2023
Please see the following graphs,
Question 1
Write a python programme to display the given data in a diagram of the following form (Fig 1).
Animal: Cat Dog Frog Others
No of Animal: 55 30 15 25
[1]: # import library
import matplotlib.pyplot as plt
# data loading
Category = ["CAT","DOG","FROG","OTHERS"]
1
Datalist = [55,30,15,25]
offsets = (0.05,0,0,0)
# graph creation
plt.pie(Datalist, labels = Category, explode=offsets, startangle=90, autopct =
,→'%1.1f%%')
# display with legends
plt.title("The Percentage of Animals")
plt.show()
[2]: # import library
import matplotlib.pyplot as plt
# data loading
Category = ["CAT","DOG","FROG","OTHERS"]
Number_of_Animals = [55,30,15,25]
# graph creation
plt.bar(Category,Number_of_Animals)
# display with legends
plt.ylabel("Number of animals")
plt.title("Number of animals")
plt.show()
2
Question 2
Write a python programme to display the given data in a sub-divided and multiple bar diagram of
the following form (Fig 2).
Year Industry Agriculture Total
2008 33.9 24.9 58.8
2009 34.5 24.1 58.6
2010 35.6 22.9 58.5
2011 36.8 21.9 58.7
2012 37.6 21.7 59.3
2013 37.5 20.6 58.1
2014 37.1 20.4 57.5
2015 36.7 21.0 57.7
2016 36.5 20.5 57.0
[3]: # import library
import matplotlib.pyplot as plt
import numpy as np
# data loading
X_category = ['2008','2009','2010','2011','2012','2013','2014','2015','2016']
Industry = [33.9,34.5,35.6,36.8,37.6,37.5,37.1,36.7,36.5]
Agriculture = [24.9,24.1,22.9,21.9,21.7,20.6,20.4,21.0,20.5]
# graph creation
plt.bar(X_category, Industry, label = "Industry")
plt.bar(X_category, Agriculture, bottom=Industry, label="Agriculture")
# display with legends
plt.ylabel("Total Percentage of Consumption")
3
plt.title("Patterns of Electricity Consumption")
plt.legend()
plt.show()
[4]: # import library
import matplotlib.pyplot as plt
import numpy as np
# data loading
X_category = ['2008','2009','2010','2011','2012','2013','2014','2015','2016']
subcategory = ["Industry","Agriculture"]
Industry = [33.9,34.5,35.6,36.8,37.6,37.5,37.1,36.7,36.5]
Agriculture = [24.9,24.1,22.9,21.9,21.7,20.6,20.4,21.0,20.5]
# graph creation
n = 9
m = 2
base = np.arange(n)
bar_width = 1/m-0.1
plt.bar(base, Industry, width = bar_width, label = subcategory[0])
plt.bar(base+bar_width, Agriculture, width = bar_width, label=subcategory[1])
# display with legends
plt.ylabel("Percentage of Consumption")
plt.title("Patterns of Electricity Consumption")
plt.legend()
plt.xticks(base, X_category)
plt.show()
4
Question 3
Write a python programme to display the given data in histograms of the following form (Fig 3).
Marks: 0-10 10-20 20-30 30-40 40-50 50-60 60-70 70-80 80-90
Boys: 5 16 20 25 30 18 10 8 0
Girls: 0 8 10 20 32 45 26 16 9
[5]: # import library
import matplotlib.pyplot as plt
# data loading
list1 = [(5,5),(15,16),(25,20),(35,25),(45,30),(55,18),(65,10),(75,8),(85,0)]
boys = sum(([val]*freq for val,freq in list1),[])
# graph creation
bins =9
range_1 =(0,90)
plt.hist(boys, bins, range_1, edgecolor = "black", color = "Green")
# display with legends
plt.xlabel("Marks")
plt.ylabel("No of Students")
plt.title("Marks of Boys")
plt.show()
5
[6]: # import library
import matplotlib.pyplot as plt
# data loading
list2 = [(5,0),(15,8),(25,10),(35,20),(45,32),(55,45),(65,26),(75,16),(85,9)]
girls = sum(([val]*freq for val,freq in list2),[])
# graph creation
bins =9
range_2 = (0,90)
plt.hist(girls, bins, range_2, edgecolor="black", color="red")
# display with legends
plt.xlabel("Marks")
plt.ylabel("No of Students")
plt.title("Marks of Girls")
plt.show()
6
Question 4
Write a python programme to display the given data in error bar charts of the following form (Fig
4).
Experiments Mean SD
Trial 1 55.7 7.0
Trial 2 77.0 14.6
Trial 3 52.0 6.3
Trial 4 58.0 15.2
Trial 5 63.0 12.3
Trial 6 56.0 8.9
[7]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Trial1","Trial2","Trial3","Trial4","Trial5","Trial6"]
mean = [55.7,77.0,52.0,58.0,63.0,56.0]
SD = [7.0,14.6,6.3,15.2,12.3,8.9]
# graph creation
plt.bar(x, mean, yerr = SD, capsize = 2, error_kw = dict(lolims = True))
# display with legends
plt.ylabel('Mean + sd')
plt.xlabel('Experiment')
plt.title("Reading from 6 trials")
plt.show()
7
[8]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Trial1","Trial2","Trial3","Trial4","Trial5","Trial6"]
mean = [55.7,77.0,52.0,58.0,63.0,56.0]
SD = [7.0,14.6,6.3,15.2,12.3,8.9]
# graph creation
plt.errorbar(x, mean, yerr = SD, fmt = 'bo', capsize = 5)
# display with legends
plt.ylabel('Mean -/+ sd')
plt.xlabel('Experiment')
plt.title("Reading from 6 trials")
plt.show()
8
Question 5
Write a python programme to display the given data in a diagram of the following form using pie
chart and bar chart (Fig 5).
Hair No of person
Blue 75
Red 50
Brown 88
Black 37
Total 250
[9]: # import library
import matplotlib.pyplot as plt
# data loading
Category = ["Blue","Red","Brown","Black"]
Datalist = [75,50,88,37]
offsets = (0,0,0.05,0)
# graph creation
plt.pie(Datalist, labels = Category, explode=offsets, startangle=90, autopct =
,→'%1.1f%%')
# display with legends
plt.title("The Percentage of Person in Different Hair Type")
plt.show()
9
[10]: # import library
import matplotlib.pyplot as plt
# data loading
Category = ["Blue","Red","Brown","Black"]
Number_of_person = [75,50,88,37]
# graph creation
plt.bar(Category, Number_of_person)
# display with legends
plt.ylabel("Number of Persons")
plt.xlabel("Hair Type")
plt.title("Number of Person and Hair Type")
plt.show()
10
Question 6
Write a python programme to display the given data in a diagram of the following form using pie
chart and bar chart (Fig 6).
Hair No of person
Blue 62
Red 25
Brown 125
Black 38
Total 250
[11]: # import library
import matplotlib.pyplot as plt
# data loading
Category = ["Blue","Red","Brown","Black"]
Datalist = [62,25,125,38]
offsets = (0,0,0,0.05)
# graph creation
plt.pie(Datalist, labels = Category, explode=offsets, autopct = '%1.1f%%',
,→shadow=True)
# display with legends
plt.title("The Percentage of Person in Different Eye Colour")
plt.show()
11
[12]: # import library
import matplotlib.pyplot as plt
# data loading
Category = ["Blue","Red","Brown","Black"]
Number_of_person = [62,25,125,38]
# graph creation
plt.bar(Category, Number_of_person)
# display with legends
plt.ylabel("Number of Persons")
plt.xlabel("Eye Colour")
plt.title("Number of Person and Eye Colour")
plt.show()
12
Question 7 & 8
Write a python programme to display the given data in a sub-divided and multiple bar diagram of
the following form (Fig 7).
Hair Eye Colour
Blue Green Brown Black Total
Blue 45 24 42 23 134
Red 20 22 48 10 100
Brown 24 13 91 45 173
Black 25 10 45 13 93
Total 114 69 226 91 500
[13]: # import library
import matplotlib.pyplot as plt
import numpy as np
# data loading
X_category = ["Blue","Red","Brown","Black"]
Blue = [45,20,24,25]
Green = [24,22,13,10]
Brown = [42,48,91,45]
Black = [23,10,45,13]
# graph creation
plt.bar(X_category, Blue, label = "Blue")
plt.bar(X_category, Green, bottom = Blue, label = "Green")
Cum_Green = np.add(Blue,Green)
plt.bar(X_category, Brown, bottom = Cum_Green, label = "Brown")
13
Cum_Brown = np.add(Cum_Green,Brown)
plt.bar(X_category, Black, bottom = Cum_Brown, label = "Black")
# display with legends
plt.ylabel("Number of person")
plt.xlabel("Hair Type")
plt.title("Number of Person, Hair Type and Eye Colour")
plt.legend(title = "Eye Colour")
plt.show()
[14]: # import library
import matplotlib.pyplot as plt
import numpy as np
# data loading
X_category = ["Blue","Red","Brown","Black"]
subcategory=["Blue","Green","Brown","Black"]
Blue = [45,20,24,25]
Green = [24,22,13,10]
Brown = [42,48,91,45]
Black = [23,10,45,13]
# graph creation
n = 4
m = 4
base = np.arange(n)
bar_width = 1/m-0.1
plt.bar(base, Blue, width = bar_width, label = subcategory[0])
plt.bar(base+bar_width, Green, width = bar_width, label=subcategory[1])
plt.bar(base+2*bar_width, Brown, width = bar_width, label=subcategory[2])
14
plt.bar(base+3*bar_width, Black, width = bar_width, label=subcategory[3])
# display with legends
plt.ylabel("Number of person")
plt.xlabel("Hair Type")
plt.title("Number of Person, Hair Type and Eye Colour")
plt.legend(title = "Eye Colour")
plt.xticks(base+1.5*bar_width, X_category)
plt.show()
Question 9
The following Table shows the lives (in hours) of four batches of electric lamps. Write a python
programme to display the given data in error bar charts of the following style (Fig 9).
Batches Mean SD
1 1680 63.9196
2 1662 58.1034
3 1636.25 103.191
4 1568.33 58.7131
[15]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Batch 1","Batch 2","Batch 3","Batch 4"]
mean = [1680,1662,1636.25,1568.33]
SD = [63.9196,58.1034,103.191,58.7131]
# graph creation
15
plt.bar(x, mean, yerr = SD, capsize = 6)
# display with legends
plt.ylabel('Life of Electric lamp')
plt.title("The lives (in hours) of Four Batches of Electric Lamps")
plt.xlabel("Batches of Electric Lamp")
plt.show()
[16]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Batch 1","Batch 2","Batch 3","Batch 4"]
mean = [1680,1662,1636.25,1568.33]
SD = [63.9196,58.1034,103.191,58.7131]
# graph creation
plt.errorbar(x, mean, yerr = SD, capsize = 6, fmt='-bo')
# display with legends
plt.ylabel('Life of Electric lamp')
plt.title("The lives (in hours) of Four Batches of Electric Lamps")
plt.xlabel("Batches of Electric Lamp")
plt.show()
16
Question 10
The following Table shows the eect of four xed types of television tube coating on the conductivity
of the tubes. Write a python program to display the given data in error bar charts of the following
style (Fig 10).
Batches Mean SD
1 58.4 2.5768
2 57.2 4.8744
3 43.6 2.4980
4 42.0 2.0000
[17]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Coating 1","Coating 2","Coating 3","Coating 4"]
mean = [58.4,57.2,43.6,42.0]
SD = [2.5768,4.8744,2.4980,2.0]
# graph creation
plt.bar(x, mean, yerr = SD, capsize = 6)
# display with legends
plt.ylabel('Conductivity of Television Tube')
plt.title('Effect of coating on the condutivity of Television Tube')
plt.xlabel('Types of Coating')
plt.show()
17
[18]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Coating 1","Coating 2","Coating 3","Coating 4"]
mean = [58.4,57.2,43.6,42.0]
SD = [2.5768,4.8744,2.4980,2.0]
# graph creation
plt.errorbar(x, mean, yerr = SD, capsize = 6, fmt='--bo')
# display with legends
plt.ylabel('Conductivity of Television Tube')
plt.title('Effect of coating on the condutivity of Television Tube')
plt.xlabel('Types of Coating')
plt.show()
18
Question 11
The following Table shows average life and standard deviation of four brands of tyres. Write a
python programme to display the given data in error bar charts of the following style (Fig 11).
Batches Mean SD
1 58.4 2.5768
2 57.2 4.8744
3 43.6 2.4980
4 42.0 2.0000
[19]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Brand 1","Brand 2","Brand 3","Brand 4"]
mean = [58.4,57.2,43.6,42.0]
SD = [2.5768,4.8744,2.4980,2.0]
# graph creation
plt.bar(x, mean, yerr = SD, capsize = 6)
# display with legends
plt.ylabel("The life of tyres (in thousand hours)")
plt.title('The life of four brands of tyres')
plt.xlabel('Brands of tyres')
plt.show()
19
[20]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Brand 1","Brand 2","Brand 3","Brand 4"]
mean = [58.4,57.2,43.6,42.0]
SD = [2.5768,4.8744,2.4980,2.0]
# graph creation
plt.errorbar(x, mean, yerr = SD, capsize = 6, fmt='--bo')
# display with legends
plt.ylabel("The life of tyres (in thousand hours)")
plt.title('The life of four brands of tyres')
plt.xlabel('Brands of tyres')
plt.show()
20
Remarks:
We can create subplots in a single diagram as follows
[21]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Brand 1","Brand 2","Brand 3","Brand 4"]
mean = [58.4,57.2,43.6,42.0]
SD = [2.5768,4.8744,2.4980,2.0]
# graph creation
fig, ax = plt.subplots(1, 2, figsize =(8, 8))
ax[0].bar(x, mean, yerr = SD, capsize = 6)
ax[1].errorbar(x, mean, yerr = SD, capsize = 6, fmt='--bo')
# display with legends
fig.suptitle('The life of four brands of tyres')
ax[0].set_ylabel("The life of tyres (in thousand hours)")
ax[0].set_xlabel('Brands of tyres')
ax[1].set_ylabel("The life of tyres (in thousand hours)")
ax[1].set_xlabel('Brands of tyres')
plt.show()
21
[22]: # import library
import matplotlib.pyplot as plt
# data loading
x = ["Brand 1","Brand 2","Brand 3","Brand 4"]
mean = [58.4,57.2,43.6,42.0]
SD = [2.5768,4.8744,2.4980,2.0]
# graph creation
fig, ax = plt.subplots(2, 1, figsize =(8, 8))
ax[0].bar(x, mean, yerr = SD, capsize = 6)
ax[1].errorbar(x, mean, yerr = SD, capsize = 6, fmt='--bo')
# display with legends
fig.suptitle('The life of four brands of tyres')
ax[0].set_ylabel("The life of tyres (in thousand hours)")
ax[0].set_xlabel('Brands of tyres')
22
ax[1].set_ylabel("The life of tyres (in thousand hours)")
ax[1].set_xlabel('Brands of tyres')
plt.show()
23