Matplotlib Axes Class (original) (raw)

In Matplotlib, **Axes class is the area where the data is plotted. It is one of the core building blocks of a plot and represents a single plot area inside a **Figure. A single **Figure can contain multiple **Axes, but each **Axes can belong to only one **Figure.

Each Axes object contains:

The **axes() function or **add_axes() method can be used to manually create axes at specific locations within a figure.

axes() function

axes() function create a new set of axes inside a figure at a custom position. The position is given as a list: **[left, bottom, width, height], where all values are between **0 and **1, showing how much space they take up compared to the full figure.

Syntax:

plt.axes([left, bottom, width, height])

**Parameter:

Example:

This code manually adds a custom-sized axes to a figure using **plt.axes() and displays it with **plt.show(). The position is set using normalized coordinates. This positions plot area centrally inside the figure window.

Python `

import matplotlib.pyplot as plt fig = plt.figure() ax = plt.axes([0.1, 0.1, 0.8, 0.8]) # [left, bottom, width, height] plt.show()

`

**Output

python-matplotlib-axes1

**Explanation:

In axes([0.1, 0.1, 0.8, 0.8])

add_axes() method

add_axes() method adds a new axes to a figure. It allows the user to set the exact position and size of the axes using [left, bottom, width, height] relative to the figure size.

Syntax:

fig.add_axes([left, bottom, width, height])

Example:

This example shows how to add an axes to a figure using **add_axes() method. The axes fills the entire figure area, as specified by **[0, 0, 1, 1] and **plt.show() displays figure window.

Python `

import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_axes([0, 0, 1, 1]) plt.show()

`

**Output

python-matplotlib-add-axes

ax.plot() Function

ax.plot() function draws lines or markers on the axes. It lets user plot data points by connecting them with lines, making it useful for visualizing trends or comparisons.

Syntax:

ax.plot(X, Y, 'CLM')

**Parameter:

Example:

This code plots three points connected by a red dashed line with circle markers using **ax.plot().

Python `

import matplotlib.pyplot as plt

fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

ax.plot([1, 2, 3], [4, 5, 6], 'ro--') # red color, circle marker, dashed line plt.show()

`

**Output

python_matplotlib_axplot

Output

**Explanation:

**Common Marker Styles:

Characters Description
. Point Marker
o Circle Marker
+ Plus Marker
s Square Marker
D Diamond Marker
H Hexagon Marker

ax.legend() Function

ax.legend() function adds a legend to the axes. It helps label and describe the plotted elements (like lines or markers) so the viewer knows what each one represents.

Syntax:

ax.legend(labels, loc)

**Parameter:

Example:

This code creates a figure with custom-sized axes and plots two lines labeled 'Line A' and 'Line B'. The **ax.legend() function is used to display a legend in the upper right corner, helping to identify each line in the plot.

Python `

import matplotlib.pyplot as plt

fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

ax.plot([1, 2, 3], [4, 5, 6], label='Line A') ax.plot([1, 2, 3], [6, 5, 4], label='Line B')

ax.legend(loc='upper right') # Add legend at the upper right plt.show()

`

**Output

python_matplotlib_addlegend

Complete Example: Plotting Sine and Cosine Curves

This code plots sine and cosine curves using Matplotlib. It creates custom axes using coordinates, then plots cosine function with blue square markers and sine function with red circle markers. A legend and title are also added to label the curves and describe the plot.

Python `

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)

Create axes using coordinates

ax = plt.axes([0.1, 0.1, 0.8, 0.8])

Plot cosine and sine

ax1 = ax.plot(X, C, 'bs:') # Blue squares with dotted line ax2 = ax.plot(X, S, 'ro-') # Red circles with solid line

ax.legend(labels=('Cosine Function', 'Sine Function'), loc='upper left') ax.set_title("Trigonometric Functions") plt.show()

`