Page 108 - Informatics_Practices_Fliipbook_Class12
P. 108
Customizing Plots by setting attributes of graph
We can also customize the points and line plot by setting the following parameters while invoking the plt.plot()
method:
● x: The x-coordinate or array of x-coordinates for the point(s) to be plotted.
● y: The y-coordinate or array of y-coordinates for the point(s) to be plotted. It should correspond to the x-coordinates
and have the same length (means the number of points of x-coordinate should match with the number of points of
y-coordinate).
● marker: Specifies the marker style for the point(s). The default marker is 'o', representing a circular marker.
● markersize: Sets the size of the marker(s). The default size is 6.
● linestyle: Determines the line style connecting the point(s). The default linestyle is '-', representing a solid line.
● color: Specifies the color of the marker(s) and line(s). The default color is 'blue'.
● linewidth: Sets the width of the line. The default linewidth is 1.0.
linestyle:
● markeredgecolor: Sets the color of the marker edges. The default color is 'blue'.
● markerfacecolor: Sets the color of the marker faces. The default color is 'blue'.
● markeredgewidth: Sets the width of the marker edges. The default linewidth is 1.0.
The following code snippet displays a line plot (Fig 3.5) using the plt.plot() method in Matplotlib. It plots the
velocity on y-axis and time along x-axis. The data points are shown using red asterisk markers of size 15. The markers
have a yellow face color and blue edge color. The line is plotted in dash-dot line style using a width of 3.5.
>>> import matplotlib.pyplot as plt
>>> time = [2, 4, 6, 8, 10]
>>> velocity = [30, 40, 60, 35, 10]
>>> plt.plot(time, velocity, 'r*-.', linewidth = 3.5, markersize = 15,
>>> markerfacecolor = 'y', markeredgecolor = 'b', markeredgewidth = 2)
>>> plt.show()
Fig 3.5: Line joining points
Setting axis label and title
Although Fig 3.5 shows the velocity of an object at different instances of time, looking at the graph gives no clues about
it. The plt module of the Matplotlib library enables us to add meaningful labels to the x and y axes. In addition,
it allows us to provide a title for the graph indicating the purpose of the graph.
94 Touchpad Informatics Practices-XII

