10/24/23, 7:46 PM Untitled9.
ipynb - Colaboratory
import matplotlib.pyplot as plt
Ploting line graph
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
plt.plot(days, temperature)
plt.show()
output
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
# plot line with x-axis, y-axis and title
plt.plot(days, temperature)
plt.title("Delhi Temperature")
plt.xlabel("days")
plt.ylabel("temperature")
plt.show()
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
# plot line with x-axis, y-axis and title
plt.plot(days, temperature)
plt.axis([0,30, 0,50]) # set axis
plt.title("Delhi Temperature")
plt.xlabel("days")
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 1/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
plt.ylabel("temperature")
plt.show()
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
# plot line with x-axis, y-axis and title
plt.plot(days, temperature, color = "g", marker = "o", linestyle= ":",
linewidth = 2, markersize = 10)
plt.title("Delhi Temperature")
plt.xlabel("days")
plt.ylabel("temperature")
plt.show()
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
from matplotlib import style # import style module
style.use("ggplot") # give ggplot parameter value to use() method
plt.plot(days, temperature, "mo--", linewidth = 3,
markersize = 10, label = "Temp line")
plt.title("Delhi Temperature", fontsize=15)
plt.xlabel("days",fontsize=13)
plt.ylabel("temperature",fontsize=13)
plt.legend(loc = 4)
plt.grid()
plt.show()
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 2/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
from matplotlib import style # import style module
style.use("ggplot") # give ggplot parameter value to use() method
plt.plot(days, temperature, "mo--", linewidth = 3,
markersize = 10, label = "Temp line")
plt.title("Delhi Temperature", fontsize=15)
plt.xlabel("days",fontsize=13)
plt.ylabel("temperature",fontsize=13)
plt.legend(loc = 4)
plt.grid(color='c', linestyle='-', linewidth=2)
plt.show()
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
delhi_tem = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
mumbai_tem = [39,39.4,40,40.7,41,42.5,43.5,44,44.9,44,45,45.1,46,47,46]
plt.plot(days, delhi_tem, "mo--", linewidth = 3,
markersize = 10, label = "Delhi tem")
plt.plot(days, mumbai_tem, "yo:", linewidth = 3,
markersize = 10, label = "Mumbai tem}")
plt.title("Delhi & Mumbai Temperature", fontsize=15)
plt.xlabel("days",fontsize=13)
plt.ylabel("temperature",fontsize=13)
plt.legend(loc = 4)
plt.show()
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 3/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
plt.plot(days, delhi_tem, "mo-", linewidth = 3,
markersize = 10, label = "Delhi tem")
plt.plot(days, mumbai_tem, "ko-", linewidth = 3,
markersize = 10, label = "Mumbai tem")
plt.title("Delhi & Mumbai Temperature", fontsize=15)
plt.xlabel("days",fontsize=13)
plt.ylabel("temperature",fontsize=13)
plt.legend(loc = 4)
plt.grid(color='c', linestyle='-', linewidth=2) # grid with parameter
plt.show()
Ploting Histograms
import matplotlib.pyplot as plt
import numpy as np
import random
ml_students_age = np.random.randint(18,45, (100))
py_students_age = np.random.randint(15,40, (100))
print(ml_students_age)
print(py_students_age)
[29 33 36 26 28 28 33 34 26 33 29 39 18 20 27 30 43 26 38 40 41 29 27 41
26 21 42 24 39 38 24 26 38 24 29 34 28 43 39 21 33 35 21 44 20 44 27 35
18 28 35 43 19 31 38 41 42 43 35 43 19 33 18 19 24 31 43 34 37 40 18 23
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 4/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
27 37 25 19 31 32 34 41 39 40 24 41 42 25 39 42 25 42 28 28 37 25 29 29
41 41 27 27]
[33 21 33 30 16 28 19 28 36 30 25 18 15 24 39 32 21 36 19 39 37 16 31 39
17 20 32 31 24 32 33 27 16 34 34 21 27 18 39 35 28 38 23 29 27 28 36 17
29 22 19 28 31 21 32 20 39 15 22 17 24 20 30 33 33 31 24 18 36 16 27 18
26 32 28 31 20 20 33 22 31 26 21 39 33 23 24 39 33 17 39 27 19 16 21 26
22 26 22 19]
plt.hist(ml_students_age)
plt.title("ML Students age histograms")
plt.xlabel("Students age cotegory")
plt.ylabel("No. Students age")
plt.show()
bins = [15,20,25,30,35,40,45] # category of ML students age on x axis
plt.figure(figsize = (16,9)) # size of histogram in 16:9 format
plt.hist(ml_students_age, bins, rwidth=0.8, histtype = "bar",
orientation='vertical', color = "m", label = "ML Student")
plt.title("ML Students age histograms")
plt.xlabel("Students age cotegory")
plt.ylabel("No. Students age")
plt.legend()
plt.show()
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 5/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
from matplotlib import style # for style
style.use("ggplot") # return grid
plt.figure(figsize = (16,9))
plt.hist([ml_students_age, py_students_age], bins, rwidth=0.8, histtype = "bar",
orientation='vertical', color = ["m", "y"], label = ["ML Student", "Py Student"])
#plt.hist(py_students_age, bins, rwidth=0.8, histtype = "bar",
# orientation='vertical', color = "y", label = "Py Student")
plt.title("ML & Py Students age histograms")
plt.xlabel("Students age cotegory")
plt.ylabel("No. Students age")
plt.legend()
plt.show()
Ploting Bar Chart
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import style
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 6/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
# Dataset of 'Indian Artificial Intelligence Production (IAIP) Class"
#classes = ["Python", "R", "Artificial Intelligence", "Machine Learning", "Data Science"]
classes = ["Python", "R", "AI", "ML", "DS"]
class1_students = [30, 10, 20, 25, 10] # out of 100 student in each class
class2_students = [40, 5, 20, 20, 10]
class3_students = [35, 5, 30, 15, 15]
plt.bar(classes, class1_students) # Plot vertical Bar Chart
<BarContainer object of 5 artists>
plt.barh(classes, class1_students) # Plot horizontal bar chart
<BarContainer object of 5 artists>
Use multiple parameters of plt.bar() method
plt.bar(classes, class1_students, width = 0.2, align = "edge", color = "y",
edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students", visible=True)
#visible = True ## bar Chart will hide
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 7/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
<BarContainer object of 5 artists>
plt.bar(classes, class1_students, width = 0.2, align = "edge", color = "y",
edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students", visible=False)
#visible = True ## bar Chart will hide
<BarContainer object of 5 artists>
Increase figure size and use style.
style.use("ggplot")
plt.figure(figsize=(16,9)) # figure size with ratio 16:9
plt.bar(classes, class1_students, width = 0.6, align = "edge", color = "k",
edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students") #visible=False
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 8/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
<BarContainer object of 5 artists>
Plot horizontal bar chart with the above specification.
plt.figure(figsize=(16,9))
plt.barh(classes, class1_students, align = "edge", color = "k",
edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students") #visible=False
<BarContainer object of 5 artists>
Decorating bar chart using multiple functions.
plt.figure(figsize=(16,9))
plt.bar(classes, class1_students, width = 0.6, align = "edge", color = "k",
edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students") #visible=False
plt.title("Bar Chart of IAIP Class", fontsize = 18)
plt.xlabel("Classes",fontsize = 15)
plt.ylabel("No. of Students", fontsize = 15)
plt.show()
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 9/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
Trying to plot two bar charts with a different dataset.
plt.figure(figsize=(16,9))
plt.bar(classes, class1_students, width = 0.2, color = "b",
label =" Class 1 Students") #visible=False
plt.bar(classes, class2_students, width = 0.2, color = "g",
label =" Class 2 Students")
plt.title("Bar Chart of IAIP Class", fontsize = 18)
plt.xlabel("Classes",fontsize = 15)
plt.ylabel("No. of Students", fontsize = 15)
plt.show()
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 10/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
The above code not generating two bar charts in one figure but they are overlapping. So use the below code to plot multiple bar charts. In below
chart plot three bar charts using three different datasets.
plt.figure(figsize=(16,9))
classes_index = np.arange(len(classes))
width = 0.2
plt.bar(classes_index, class1_students, width , color = "b",
label =" Class 1 Students") #visible=False
plt.bar(classes_index + width, class2_students, width , color = "g",
label =" Class 2 Students")
plt.bar(classes_index + width + width, class3_students, width , color = "y",
label =" Class 2 Students")
plt.xticks(classes_index + width, classes, rotation = 20)
plt.title("Bar Chart of IAIP Class", fontsize = 18)
plt.xlabel("Classes",fontsize = 15)
plt.ylabel("No. of Students", fontsize = 15)
plt.show()
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 11/13
10/24/23, 7:46 PM Untitled9.ipynb - Colaboratory
Plot Matplotlib Horizontal bar chart with the above specification.
plt.figure(figsize=(16,9))
classes_index = np.arange(len(classes))
width = 0.2
plt.barh(classes_index, class1_students, width , color = "b",
label =" Class 1 Students") #visible=False
plt.barh(classes_index + width, class2_students, width , color = "g",
label =" Class 2 Students")
plt.barh(classes_index + width + width, class3_students, width , color = "y",
label =" Class 3 Students")
plt.yticks(classes_index + width, classes, rotation = 20)
plt.title("Bar Chart of IAIP Class", fontsize = 18)
plt.ylabel("Classes",fontsize = 15)
plt.xlabel("No. of Students", fontsize = 15)
plt.legend()
plt.show()
Double-click (or enter) to edit
https://colab.research.google.com/drive/1Fqc3o0NSdZP8rNbezsHx94OSnwy7EDCO?authuser=0#scrollTo=WgmacUIG9Xvl&printMode=true 12/13