Page 109 - Informatics_Practices_Fliipbook_Class12
P. 109
To name the axis and assign a title to a graph, the Matplotlib offers the following methods:
● plt.xlabel(): This function is used to set the label for the x-axis of the plot. It accepts a string as an argument
that denotes the label for the x-axis.
● plt.ylabel(): This function is used to set the label for the y-axis of the plot. It accepts a string as an argument.
that denotes the label for the y-axis.
● plt.title(): This function is used to set the title for the plot. It takes a string as an argument that denotes the
title of the graph.
The following code snippet labels x-axis and y-axis as time and velocity, respectively. It also assigns the caption Time
vs. Velocity to the graph (Fig 3.6).
>>> 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.show()
Fig 3.6: Time vs. Velocity
To name the axes and assign a title to a graph, the Matplotlib offers the following methods:
plt.xlabel(): This function is used to set the label for the x-axis of the plot. It accepts a string as an argument
that denotes the label for the x-axis.
plt.ylabel(): This function is used to set the label for the y-axis of the plot. It accepts a string as an argument
that denotes the label for the y-axis.
plt.title(): This function is used to set the title for the plot. It takes a string as an argument that denotes the
title of the graph.
Using Default Values along x-axis
While invoking the method plt.plot, sometimes it is convenient to use the standard coordinates on x-axis: 0, 1, 2,
3, and so on. Thus, it is enough to provide only the sequence of y-coordinates. This default feature makes it convenient
to draw simple line plots without the need to specify the x-coordinates explicitly.
Data Visualization 95

