For first plot using matplotlib library we will create a line graph. The line graph is created using the plot function of the matplotlib library. The plt.plot() function in general form is written as:
scalex = True - this parameter automatically scales the x-axis to the data if set to True. If False, it will not rescale even if the data exceeds the current axis limits.
scaley = True - this parameter works the same as the scalex but for the y-axis. If True, if automatically scales the y-axis to the data.
data = None - this allows you to pass a data structure (dictionary or pandas DataFrame), so that instead of passing actual arrays or sequences for \(x\) and \(y\), you can refer to the names or keys of the data. This is useful for plotting irectlyfrom complex data structures i.e.
data = {'x': [1,2,3,4], 'y':[10,20,25,30]}
plot('x', 'y', data=data)
**kwargs - These are additional keyword arguments that can be used to customize the plot such as: color, linestyle, marker, label...
In this example we will plot the line plot that connects points with the following x and y coordinates.
\begin{eqnarray}
x &=& [1,2,3,4] \\ \nonumber
y &=& [23,445,567,3]
\end{eqnarray}
The first step is to define the required libraries. If you have correctly installed the matplotlib library then type in the following code in your Python script.
import matplotlib.pyplot as plt
The second step is to define the data points (coordinates) that will be plotted using the line plot.
x = [1,2,3,4,5,6]
y = [2,4,7,8,10,12]
When the library and the data points are defined then we can define the required matplotlib functions to show the line graph. In this case we will need to define the figure size using plt.figure(figsize=(12,8)) function,plt.plot for creating the line plot, grid to display the grid to improve the readability, xlabel and ylabel to show the labels of the x and y axes, and finally the show function to display the line plot.
The entire code created in this example is shwon below.
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6]
y = [2,4,7,8,10,12]
plt.figure(figsize=(12,8))
plt.plot(x,y)
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
When the previous code is executed the line plot is generated as shwon in Figure.
Figure 1 - Line plot created using pyplot.plot() funciton.
The previous block of code responsible for creating line plot in Figure 1 consist of the following matplotlib.pyplot functions:
plt.figure(figsize=(12,8))
plt.figure()- the plt is the common alias for the matplotlib pyplot module (it was previously explained when we imported the matplotlib library).
figure() - is the function that creates new figure in which you can plot your data If you forget to define it explicitly, the matplotlib will create one automatically when you start plotting. However the default figsize would be 6.4 by 4.8 which is width and height in inches.
figsize=(12,8) - - the figsize is the parrameter that specifies the sie fo the figure in inches. The parameter is a tuple that contains two values:
width - the first value in the tuple (12) represents the width of the figure.
height - the second vlaue (8) represents the height of the figure
numbers 12 and 8 - the size is given in inches beacuse Matplotlib uses inches as the unit of measure for figure dimensions. A figure with a size of (12,8) will be 12 inches wide and 8 inches tall.
dots per inch - the actual number of pixels in the figure is determined by the DPI setting and its 100 DPI by default. So , a figure with a 12x8 would have 1200 x 800 pixle size (12 inches * 100 DPI by 8 inches * 100 DPI).
plt.plot(x,y) - the plot() function is the pyplot function used for creating the 2D plots, usually a line plot, based on the provided data. The x and y are data points that you want to plot. The x represents the data for the x-axis.It is usally a list, array, or other iterable containing numerical values.y represents the data for the y-axis. As the x the y can also be a list, array, or another iterable containing numerical values. However, the length of the y must be the same as x.The plot function will connect each pair of points (x[i],y[i]) whit a line, creating a continuous line plot. The command matches each element in x with the corresponding element in y. For example x = [1,2,3,4,5,6] and y = [2,4,7,8,10,12] the function plots the points (1,2), (2,4), (3,7), (4,8), (5,10), and (6,12) and connects them with a line.
plt.grid(True) - This function is used to toggle the visibility of the grid lines on the plot. The True argument turns on the grid lines. This means the grid lines will be displayed on the plot. In case the False value is inside the brackets it will turn off the grid lines. The puprose of grid lines is to enhance the readability i.e. grid lines help improve the readability of the plot by making it easire to aling and iterpret data points, especially when the plot contains multple lines or data points.
plt.xlabel('x'), plt.ylabel('y')- xlabel and ylabel are functions used to set a label for x-axis and y axis of the current plot. The x or y argument are labels you want to display along the x and y-axis respectively. For example try changing the xlabel argument to "time (s)" and the ylabel argument to "Temperature [K]" and see what happens.
plt.show() - is the matplotlib command that displays the current plot or figure. It opens a window with the plot, allowing you to visualize the data. This function is essential for creating plots in scripts or interactive enviroments, as it renders the plot on the screen. After the commnad is called, the figure is displayed and the script execution continues or it ends if this sis the last command in the script.