Page 117 - Informatics_Practices_Fliipbook_Class12
P. 117
11 for value in x:
12 y3.append(value**3)
13 plt.plot(x, y1, 'ro-', label='linear')
14 plt.plot(x, y2, 'b<--', label='quadratic')
15 plt.plot(x, y3, 'g*-.', label='cubic')
16 plt.xlabel('x')
17 plt.ylabel('')
18 plt.title("Linear, Quadratic, and Cubic Plots")
19 plt.legend()
20 plt.savefig("Linear, Quadratic, and Cubic Plots")
21 plt.show()
Fig 3.16: Linear, Quadratic, and Cubic Plots
When, multiple graphs are included in the same figure, this information is included by specifying the labels while
invoking the plt.plot() function for each graph.
Consider the following three lists:
ages = [15, 20, 25, 30, 35, 40, 45, 50, 55, 60]
heights = [160, 165, 170, 175, 180, 185, 190, 195, 200, 205]
weights = [45, 55, 60, 65, 70, 75, 80, 85, 90, 95]
Draw a graph that depicts age vs weight and age vs height in the same graph:
3.1.5 Plotting Functions in Different Plots in Same Graph
We often encounter scenarios where we need to plot multiple functions as subgraphs of a graph to compare their
behaviours or analyze their relationships.
Unlike earlier, to plot functions like f(x) = x, f(x) = x^2, and f(x) = x^3 on the same graph but in different
sub plots, we can utilize the subplot() function of matplotlib to create multiple subplots for separate functions
within a single figure.
Data Visualization 103

