Page 110 - Informatics_Practices_Fliipbook_Class12
P. 110
For example, if we just want to display the velocity at time instances 0, 1, 2, 3, and 4, we could use the default values
of the x coordinates: 0, 1, 2, 3, and 4, as shown below in Fig 3.7.
>>> import matplotlib.pyplot as plt
>>> velocity = [10, 40, 60, 35, 10]
>>> plt.plot(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.show()
Fig 3.7: Time vs. Velocity
Saving Figure
Oftentimes, it is necessary to save the graphs for future reference. Indeed, the graphs must be referred by people who
have nothing to do with the matplotlib library or even programming. The matplotlib library provides the function
plt.savefig() for saving a graph. Name of the file (including the file format) is provided as the input argument to the
function plt.savefig(). By default, the figure will be saved in the current working directory. To save the file in some
other directory, you may specify the complete path name instead of just the file name. For saving the figure in high
resolution, we can set the optional parameter dpi (dots per inch), by invoking the following statement before plt.
show():
plt.savefig('Time vs Velocity.png', dpi = 1000)
The matplotlib library provides the function plt.savefig() for saving a graph. Name of the file (including the
file format) is provided as the input argument to the function plt.savefig(). By default, the figure will be saved
in the current working directory.
Adding Ticks and Creating Grid
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.)
to method xticks() and yticks(). In the following example, we use x-ticks in steps of 2 and y-ticks in steps of 5.
96 Touchpad Informatics Practices-XII

