Page 111 - Informatics_Practices_Fliipbook_Class12
P. 111
Further, we use the highest values in the lists time and velocity to define range of values for the ticks (Fig 3.8).
>>> import matplotlib.pyplot as plt
>>> time = [2, 4, 6, 8, 10]
>>> velocity = [30, 40, 60, 35, 10]
>>> plt.plot(time, velocity, 'r*-.', linewidth = 1.5, markersize = 15, markerfacecolor = 'y',
markeredgecolor = 'b', markeredgewidth = 1)
>>> plt.xlabel('Time')
>>> plt.ylabel('Velocity')
>>> plt.title('Time vs. Velocity')
>>> plt.xticks(range(0, max(time), 2))
>>> plt.yticks(range(0, max(velocity), 5))
>>> plt.grid()
>>> plt.show()
Fig 3.8: Time vs. Velocity
The statement plt.xticks(range(0, max(time), 2)), sets the tick positions on the x-axis at intervals of 2
units, ranging from 0 to 10. Similarly, the statement plt.yticks(range(0, max(velocity), 5)), sets the
tick positions on the y-axis at intervals of 5 units, ranging from 0 to 60. Finally, the statement plt.grid(), adds a
grid to the plot by drawing the horizontal and vertical line segments along the ticks.
To enhance the readability of a graph, the plt module allows us to display a rectangular grid using the method
plt.grid().
The ticks on the x-axis mark the positions of the vertical lines of the grid. The ticks on the y-axis mark the positions of
the horizontal lines of the grid. The position of ticks is typically described using a sequence (using a list, range(),
etc.) provided as input to methods xticks() and yticks().
C T 02 Modify the above code snippet to draw the above graph using the line segments with dotted
lines.
Data Visualization 97

