- What is a Line Plot?
- Components of a basic plot
- Plotting a graph
- Line Properties
What is a Line Plot?
The pyplot module in matplotlib supports a variety of plots such as – line plot, pie chart, bar chart, histogram, scatter plot etc., The module defines methods that are used to render various plots. In this tutorial, we will discuss about line plots.
A line plot is created by connecting the values in the input data with straight lines. Line plots are used to determine the relation between two datasets. A dataset is a collection of values. Each dataset is plotted along an axis ie., x and y axis. In order to draw a line plot, we call the plot function defined in pyplot module. We pass two arguments (arrays or lists) to the plot function, the first argument denotes the x-coordinates, second argument denotes the y-coordinates. The plot function plots the data points (x1,y1), (x2,y2) and so on defined in the input datasets and by default, draws a line between these data points. Before drawing a plot, let us the see the components that make up a basic plot.
Components of a basic plot
A basic plot is made up of the following components:
- Title – Title describes the information that we want to convey using the graph.
- Label – Label is a short description of the datasets being plotted.
- Scales – Scales determine the reference points for data displayed on the graph.
- Points – Points in a graph represent the input data in the form of x-coordinate and y-coordinate (x1,y1).
- Lines – Lines are used to connect points to highlight the change in values.
Plotting a graph
Creating a line plot by passing two arrays
Matplotlib makes extensive use of the Numpy library which contains a number of mathematical functions which can be used to perform various mathematical operations. We need to import Matplotlib and Numpy libraries before making any calls to the routines defined in them. The below example demonstrates creation of a line plot by passing two numpy arrays x and y as arguments to the plot function.
from matplotlib import pyplot as plt import numpy as np x = np.linspace(0,10,10) y = np.linspace(0,10,10) plt.title('First Plot') plt.xlabel('x-axis') plt.ylabel('y-axis') plt.plot(x,y,marker='o') plt.show()
In order to render the above plot, we simply passed two arrays x,y to the plot function. We can see that the plot function has:
- Drawn the x and y axis.
- Marked evenly spaced scales(tick marks) on both the axes.
- Plotted the data points.
- Connected the data points with lines.
- Added the title, xlabel, ylabel.
Before executing the above mentioned steps, the plot function first creates a figure object. Figure objects are the individual windows on the screen in which Matplotlib displays the graphical output. It is a container for the graphical output. In the Jupyter NB, figures rendered by the Matplotlib library, are included inline. The plot function implicitly creates a figure object and then plots the graph. So, we do not have to call any other function to instantiate a figure object to render a plot when using the plot function. The standard size of a figure object is 8 inches wide by 6 inches high.
Say, for example, we have a requirement to create a figure with a specified size (4 inches wide, 4 inches high). For this, we need to call the Figure method defined in the pyplot module explicitly. The ‘figsize’ parameter of this method allows us to specify the width and height of a figure in unit inches and new a figure will be created. In order to render a plot, call the plot function.
The savefig() method saves the figure to a data file with a name specified by the string argument. The filename can be a full path and can also include a file extension if needed.
plt.figure(num=1,figsize=(4,4),dpi=100) plt.plot(x,y,marker='o') plt.savefig('second_plot.png') plt.close()
Creating a line plot by passing a single array
We can pass a single dataset or an array to the plot function as shown in cell below. The plot function then considers this array as y-coordinates and generates the x-coordinates for plotting. The plot function uses the values 0, 1, …, N-1 as the x coordinates where ‘N’ is the size of the y array.
plt.title('Second Plot') plt.xlabel('x-axis') plt.ylabel('y-axis') plt.plot(y) plt.show()
Multiple plots on the same graph
We can plot multiple plots on the same graph by calling the plot function for each dataset pair, this is useful to compare the plots. Each plot is rendered on top of another plot. Notice how Matplotlib applies a different color to each plot. The plots share the figure, x and y axis.
plt.plot(x,y) plt.plot(x,x**2) plt.plot(x,x**1/2) plt.title('Multiple Plots') plt.xlabel('x-axis') plt.ylabel('y-axis') plt.show()
The above code can be re-written as follows, that is by passing each dataset pair in an order as arguments to the plot function.
plt.plot(x,y,x,x**2,x,x**1/2) plt.title('Multiple Plots2') plt.xlabel('x-axis') plt.ylabel('y-axis') plt.show()
A line drawn on a graph has several properties such as color, width, style, transparency etc., these properties can be customized as per our requirement when we call the plot function.
plt.plot(x,y,color='c',marker='o',linewidth=2,linestyle='--',alpha=0.5,label='line1') plt.plot(x,x**2,color='#90BC38',marker='D',linewidth=2,alpha=0.5,label='line2') plt.legend() plt.show(
Colors in data visualization are used to enhance the look of the graphs, communicate the information clearly and to distinguish one set of data from another. The following basic colors are defined in Matplotlib.
Matplotlib also supports HEX colors. Web designers and developers use HEX colors in web designing. A HEX color is represented as a six-digit combination of numbers and letters defined by the amount of red, green and blue (RGB) that makes up the color.
Linestyle specifies whether the line is solid, dashed etc.,
Markers on a line plot are used to highlight particular data points.
A legend is a box associating labels(text) with lines on a graph. The legend() method is used to add a legend to the plot. This method can be called in multiple ways:
- plt.legend() – When no arguments are passed to the legend() method, the plots to be added in the legend are automatically detected, and the corresponding labels are used in the legend.
- plt.legend([‘label1’, ‘label2’, ‘label3’]) – The legend method can also be called by passing a list of string labels, where each string is used as a label for the plots in the order they were created. This method can be used to create a legend for the plots already existing on the axes. Note: This way of using the legend method is often discouraged because you should remember the order in which the plots are created which can be confusing.
- plt.legend([plot1,plot2,plot3],[‘label1′,’label2′,’label3’]) – We can explicitly specify the plots and labels by passing the list of plots followed by the list of string labels arranged in order to the legend method.