Page 116 - Informatics_Practices_Fliipbook_Class12
P. 116

Now we can easily compare the scores obtained by the students under two different conditions, i.e., studying in a quiet
        room vs studying in a noisy room. However, note that the graph does not tell us which line graph corresponds to which
        condition. 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. These labels are displayed in a box within the graph when
        the function plt.legend() is invoked. This is illustrated below (Fig 3.15):
         >>> import matplotlib.pyplot as plt
         >>> timeSpentStudying = [1, 2, 3, 4, 5]
         >>> quietRoomScores = [63, 75, 82, 87, 93]
         >>> noisyRoomScores = [58, 62, 68, 69, 75]
         >>> plt.plot(timeSpentStudying, quietRoomScores, 'ro-', label='Quiet room')
         >>> plt.plot(timeSpentStudying, noisyRoomScores, 'b<-',  label='Noisy room')
         >>> plt.xlabel('No of hours studied')
         >>> plt.ylabel('Score')
         >>> plt.title("Performance in a Noisy Room vs Quiet Room")
         >>> plt.xticks(timeSpentStudying)
         >>> plt.legend()
         >>> plt.show()
































                                       Fig 3.15: Performance in a Noisy Room vs Quiet Room
        In the following graph (Fig 3.16), we display the linear, quadratic, and cubic functions, for the values in the interval [0,
        2.0] in steps of 0.2 (please see Program 1).

         Program 1: To plot linear, quadratic, and cubic functions

          01 import matplotlib.pyplot as plt
          02 x = [0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
          03 # Linear function
          04 y1 = x
          05 # Compute Squares
          06 y2 = []
          07 for value in x:
          08   y2.append(value**2)
          09 # Compute cubes
          10 y3 = []




          102  Touchpad Informatics Practices-XII
   111   112   113   114   115   116   117   118   119   120   121