Wednesday, July 3, 2024
HomeLanguagesPythonMatplotlib.axes.Axes.get_aspect() in Python

Matplotlib.axes.Axes.get_aspect() in Python

Matplotlib is a library in Python and it is numerical ā€“ mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.get_aspect() Function

The Axes.get_aspect() function in axes module of matplotlib library is used to get the aspect of the axis scaling, i.e. the ratio of y-unit to x-unit

Syntax:Axes.get_aspect(self)

Parameters: This method does not accepts any parameters.

Return value: This method returns the aspect value.

Below examples illustrate the matplotlib.axes.Axes.get_aspect() function in matplotlib.axes:

Example 1:




# ImpleIn Reviewtation of matplotlib functionĀ Ā 
import matplotlib.pyplot as plt
Ā Ā Ā 
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_xscale("log")
ax1.set_yscale("log")
ax1.set_adjustable("datalim")
ax1.plot([1, 3, 34, 4, 46, 3, 7, 45, 10],
Ā Ā Ā Ā Ā Ā Ā Ā Ā [1, 9, 27, 8, 29, 84, 78, 19, 48],
Ā Ā Ā Ā Ā Ā Ā Ā Ā "o-", color ="green")
Ā Ā 
ax1.set_xlim(1e-1, 1e2)
ax1.set_ylim(1, 1e2)
Ā Ā Ā 
ax2.set_xscale("log")
ax2.set_yscale("log")
ax2.set_adjustable("datalim")
ax2.plot([1, 3, 34, 4, 46, 3, 7, 45, 10],
Ā Ā Ā Ā Ā Ā Ā Ā Ā [1, 9, 27, 8, 29, 84, 78, 19, 48],
Ā Ā Ā Ā Ā Ā Ā Ā Ā "o-", color ="green")
Ā Ā 
ax2.set_xlim(1e-1, 1e2)
ax2.set_ylim(1, 1e2)
ax2.set_aspect(2)
Ā Ā 
w = ax1.get_aspect()
w1 = ax2.get_aspect()
Ā Ā 
ax1.set_title("Axes 1\n Ratio : " +str(w))
ax2.set_title("Axes 2\n Ratio : " +str(w1))
Ā Ā 
fig.suptitle('matplotlib.axes.Axes.get_aspect() \
function Example\n\n', fontweight ="bold")
fig.canvas.draw()
plt.show()


Output:

Example 2:




# ImpleIn Reviewtation of matplotlib functionĀ Ā 
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
Ā Ā Ā 
n_angles = 36
n_radii = 10
min_radius = 2
radii = np.linspace(min_radius, 0.95, n_radii)
Ā Ā Ā 
angles = np.linspace(0, 2 * np.pi, n_angles,Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā endpoint = False)
Ā Ā 
angles = np.repeat(angles[..., np.newaxis],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā n_radii, axis = 1)
Ā Ā 
angles[:, 1::2] += 2 * np.pi / n_angles
Ā Ā Ā 
x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
Ā Ā Ā 
triang = tri.Triangulation(x, y)
Ā Ā Ā 
triang.set_mask(np.hypot(x[triang.triangles].mean(axis = 1),
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[triang.triangles].mean(axis = 1))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā < min_radius)
fig, (ax, ax1) = plt.subplots(1, 2)
Ā Ā Ā 
ax.triplot(triang, 'bo-', lw = 1, color = "green")
Ā Ā Ā 
ax1.set_aspect('equal')
ax1.triplot(triang, 'bo-', lw = 1, color = "green")
Ā Ā 
w = ax.get_aspect()
w1 = ax1.get_aspect()
Ā Ā 
ax.set_title("Axes 1\n Ratio : " +str(w))
ax1.set_title("Axes 2\n Ratio : " +str(w1))
Ā Ā 
fig.suptitle('matplotlib.axes.Axes.get_aspect() \
function Example\n\n', fontweight ="bold")
fig.canvas.draw()
plt.show()


Output:

Thapelo Manthata
Iā€™m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments