Friday, December 27, 2024
Google search engine
HomeLanguagesMatplotlib.axes.Axes.autoscale() in Python

Matplotlib.axes.Axes.autoscale() 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.autoscale() Function

The Axes.autoscale() function in axes module of matplotlib library is used to autoscale the axis view to the data (toggle).

Syntax: Axes.autoscale(self, enable=True, axis=’both’, tight=None)

Parameters: This method accepts the following parameters.

  • enable : If this parameter is True (default) turns autoscaling on, False turns it off.
  • axis: This parameter is used to which axis to be operate on. {‘both’, ‘x’, ‘y’}
  • tight: This parameter is forwarded to autoscale_view.

Return value: This method does not return any value.

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

Example 1:




# Implementation of matplotlib function  
import numpy as np
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import matplotlib.pyplot as plt
  
  
vertices = []
codes = []
  
codes = [Path.MOVETO] + [Path.LINETO]*3 + [Path.CLOSEPOLY]
vertices = [(1, 1), (1, 2), (2, 2), 
            (2, 1), (0, 0)]
  
codes += [Path.MOVETO] + [Path.LINETO]*2 + [Path.CLOSEPOLY]
vertices += [(4, 4), (5, 5), (5, 4), 
             (0, 0)]
  
vertices = np.array(vertices, float)
path = Path(vertices, codes)
  
pathpatch = PathPatch(path, facecolor ='None',
                      edgecolor ='green')
  
fig, ax = plt.subplots()
ax.add_patch(pathpatch)
ax.autoscale()
  
fig.suptitle('matplotlib.axes.Axes.autoscale() \
function Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()


Output:

Example 2:




# Implementation of matplotlib function  
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import EllipseCollection
  
x = np.arange(10)
y = np.arange(15)
X, Y = np.meshgrid(x, y)
  
XY = np.column_stack((X.ravel(), Y.ravel()))
  
fig, ax = plt.subplots()
  
ec = EllipseCollection(10, 10, 5, units ='y',
                       offsets = XY * 0.5,
                       transOffset = ax.transData,
                       cmap ="inferno")
  
ec.set_array((X * Y).ravel())
ax.add_collection(ec)
ax.autoscale_view()
ax.set_xlabel('X')
ax.set_ylabel('y')
cbar = plt.colorbar(ec)
cbar.set_label('X + Y')
  
fig.suptitle('matplotlib.axes.Axes.autoscale() function \
Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()


Output:

RELATED ARTICLES

Most Popular

Recent Comments