Matplotlib.axes.Axes.set_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.
#Sample Code

filter_none

edit
close

play_arrow

link
brightness_4
code

# Implementation of matplotlib function
      
import matplotlib.pyplot as plt
import numpy as np
    
# make an agg figure
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.set_title('matplotlib.axes.Axes function')
fig.canvas.draw()
plt.show()

chevron_right


Output:

matplotlib.axes.Axes.set_aspect() Function

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

Syntax:



Axes.set_aspect(self, aspect, adjustable=None, anchor=None, share=False)

Parameters: This method accepts the following parameters.

  • aspect : This parameter accepts the following value {‘auto’, ‘equal’} or num.
  • adjustable : This defines which parameter will be adjusted to meet the required aspect.
  • anchor : This parameter is used to define where the Axes will be drawn if there is extra space due to aspect constraints.
  • share: This parameter is used to apply the settings to all shared Axes.

Return value: This method does not return any value.

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

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)
ax1.set_title("No set_aspect")
  
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)
ax2.set_title("set_aspect value = 2")
  
fig.suptitle('matplotlib.axes.Axes.set_aspect() \
function Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()

chevron_right


Output:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# ImpleIn Reviewtation of matplotlib function  
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
  
n_angles = 20
n_radii = 10
min_radius = 2
radii = np.linspace(min_radius, 0.95, n_radii)
  
angles = np.linspace(0, 4 * np.pi, n_angles,
                     endpoint = False)
angles = np.repeat(angles[..., np.newaxis],
                   n_radii, axis = 1)
angles[:, 1::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")
ax.set_title("No set_aspect")
  
ax1.set_aspect('equal')
ax1.triplot(triang, 'bo-', lw = 1, color = "green")
ax1.set_title("set_aspect value ='equal'")
  
fig.suptitle('matplotlib.axes.Axes.set_aspect() \
function Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.