Page 121 - Informatics_Practices_Fliipbook_Class12
P. 121
We can utilise the subplot() function of matplotlib to create multiple subplots for separate functions within a
single figure. The syntax for subplot() is as follows:
subplot(rowNum, colNum, FigNum)
C T 03 Complete the following code that plots sine and cosine graphs on two different subplots in same graph
import matplotlib.pyplot as plt
import math
def sineCurve():
'''
Objective: To plot sine function
Input Parameter: None
Return Value: None
'''
plt.subplot(2, 1, 1)
degrees = range(0, 360 + 1)
sineValues = [math.sin(math.radians(x)) for x in degrees]
plt.plot(degrees, sineValues)
plt.xlabel('Degree')
plt.ylabel('Sine Values')
plt.title('Sine Curve')
plt.grid()
def cosineCurve():
'''
Objective: To plot cosine function
Input Parameter: None
Return Value: None
'''
plt.subplot(2,1,?) # PLEASE REPLACE ? with appropriate plot number
degrees = range(0, 360 + 1)
cosineValues = # PLEASE FILL THEBLANK BY APPROPRIATE EXPRESSION
plt.plot(cosineValues)
plt.xlabel('Degree')
plt.ylabel('Cosine Values')
plt.title('Cosine Curve')
plt.grid()
sineCurve()
cosineCurve()
plt.tight_layout()
plt.show()
3.2 Bar Graph
Bar graphs, often known as bar charts, are a common method for displaying categorical data. They depict the
relationship between several categories and their related values visually. We will draw bar graphs using Python's
Matplotlib package. In the following example, we will visualize the data about the number of persons playing four
sports, namely, hockey, volley ball, badminton, or cricket.
Data Visualization 107

