Matplotlib is one of the Python packages which is used for data visualization. You can use the NumPy library to convert data into an array and numerical mathematics extension of Python. Matplotlib library is used for making 2D plots from data in arrays.
Axes is the most basic and flexible unit for creating sub-plots. Axes allow placement of plots at any location in the figure. A given figure can contain many axes, but a given axes object can only be in one figure. The axes contain two axis objects 2D as well as, three-axis objects in the case of 3D. Let’s look at some basic functions of this class.
axes() function creates axes object with argument, where argument is a list of 4 elements [left, bottom, width, height]. Let us now take a brief look to understand the axes() function.
axes([left, bottom, width, height])
axes([0.1, 0.1, 0.8, 0.8]), the first ‘0.1’ refers to the distance between the left side axis and border of the figure window is 10%, of the total width of the figure window. The second ‘0.1’ refers to the distance between the bottom side axis and the border of the figure window is 10%, of the total height of the figure window. The first ‘0.8’ means the axes width from left to right is 80% and the latter ‘0.8’ means the axes height from the bottom to the top is 80%.
Alternatively, you can also add the axes object to the figure by calling the
add_axes() method. It returns the axes object and adds axes at position [left, bottom, width, height] where all quantities are in fractions of figure width and height.
add_axes([left, bottom, width, height])
Adding legend to the plot figure can be done by calling the legend() function of the axes class. It consists of three arguments.
ax.legend(handles, labels, loc)
Where labels refers to a sequence of string and handles, a sequence of Line2D or Patch instances, loc can be a string or an integer specifying the location of legend.
plot() function of the axes class plots the values of one array versus another as line or marker.
Syntax : plt.plot(X, Y, ‘CLM’)
X is x-axis.
Y is y-axis.
‘CLM’ stands for Color, Line and Marker.
Note: Line can be of different styles such as dotted line
(':'), dashed line
('—'), solid line
('-') and many more.
Example: The following example shows the graph of sine and cosine functions.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course