The image’s region with the data space is commonly known as Axes object. The flexible and basic unit for creating sub-plots is Axes.
- With the help of axes, one can plot at any location in the Figure; thus it provides you the creation of sub-plots in a Flexible way.
- Any Figure that is given may contain many axes, but a given Axes object can only be in a figure.
- In the case of two-dimension, the Axes contain two axis objects and in the case of 3D, the Axes contain three-axis objects.
- To add an Axes object to the figure you just need to make a call to
add_axes()method will return the axes object and adds axes at the position rect [left, bottom, width, height] where all these quantities are present in fractions of figure width and height.
This function is mainly used to create an axes object with argument.
- The argument is mainly a list of 4 elements [left, bottom, width, height]
Given below is a basic syntax of this function:
axes([left, bottom, width, height])
Let us take a look at the simplest example where we will use the axes() function:
import matplotlib.pyplot as plt fig = plt.figure() #[left, bottom, width, height] ax = plt.axes([0.2, 0.2, 0.9, 0.9])
The output for the same is as follows:
Explanation of the above example:
- In the above code,
axes([0.2, 0.2, 0.9, 0.9]), where the first ‘0.2’ indicates the distance between the left side axis and the border of the figure window that is 20%, of the total width of the figure window.
- The second ‘0.2’ is indicating the distance between the bottom side axis and the border of the figure window that is 20%, of the total height of the figure window.
- Also, The first ‘0.9’ indicates that the axes width from left to right is 90% and
- the latter ‘0.9’ indicates the axes height from the bottom to the top is 80%.
Member Functions in Axes Class
In the following section, we have member functions of axes class which are used to add different elements to the plot:
To add a legend to the plot figure the
legend() method of axes class is used.
Here is the syntax:
ax.legend(handles, labels, loc)
This function takes three parameters:
- The parameter labels is used to indicate a sequence of strings and it mainly handles a sequence of Line2D.
- The parameter loc can either be a string or an integer which mainly specifies the legend location.
|The Location string||The Location code|
Below we have a basic example where we will use
import matplotlib.pyplot as plt fig = plt.figure() #[left, bottom, width, height] ax = plt.axes([0.2, 0.2, 0.8, 0.8]) ax.legend(labels = ('label1', 'label2'), loc = 'center right')
The output for this code is as follows:
If there is a need then you can also add the axes object to the figure by just by making a call to the
add_axes() method (thus it is an alternative method).
This method will return the axes object and adds axes at position [left, bottom, width, height] where all these quantities are in fractions of figure width and height.
Here is the syntax:
add_axes([left, bottom, width, height])
let us cover an example using this method:
import matplotlib.pyplot as plt fig = plt.figure() #[left, bottom, width, height] ax = fig.add_axes([1, 1, 1, 1])
The output for the above code example is as follows:
It is the most basic method of the axes class that is used to plot values of one array versus another as lines or markers.
This method can have an optional format string argument which is mainly used to specify color, style and size of line and marker.
Using Color codes:
To specify colors we will use color codes:
Using Marker codes:
To specify style of marker we will use this:
Using Line styles:
Various line styles you can use are as follows:
Here is the syntax:
plt.plot(X, Y, ‘CLM’)
X: This parameter denotes the x-axis.
Y: This parameter denotes the y-axis
CLM: stands for color, line, and Marker.
Let us cover an example for the above-explained function:
import matplotlib.pyplot as plt import numpy as np X = np.linspace(-np.pi, np.pi, 15) C = np.cos(X) S = np.sin(X) # [left, bottom, width, height] ax = plt.axes([0.1, 0.1, 0.8, 0.8]) ax1 = ax.plot(X, C, 'mx:') ax2 = ax.plot(X, S, 'cD-') ax.legend(labels = ('Cosine Function', 'Sine Function'),loc = 'upper left') ax.set_title("Trigonometric Functions in Mathematics") plt.show()
The output for the above code snippet is as shown below: