Open In App

Matplotlib.axes.Axes.get_position() in Python

Last Updated : 19 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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_position() Function

The Axes.set_position() function in axes module of matplotlib library is used to Get a copy of the axes rectangle as a Bbox..

Syntax: Axes.get_position(self, original=False)

Parameters:

  • original : This parameter is used to return the original position if true. Otherwise return the active position.

Return value: This method returns a copy of the axes rectangle as a Bbox.

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

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
   
x = np.arange(10)
y = [2, 4, 6, 14, 15, 16, 17, 16, 18, 20]
y2 = [10, 11, 12, 13, 8, 10, 12, 14, 18, 19]
   
fig, ax1 = plt.subplots()
   
ax1.plot(x, y, "go-", label ='Line 1', )
ax1.plot(x, y2, "o-", label ='Line 2')
  
chartBox = ax1.get_position()
x, y, w, h = chartBox.x0, chartBox.y0, chartBox.width, chartBox.height
  
ax1.text(0, 20, "Bbox - xmin : "+str(x), 
         fontweight ="bold")
ax1.text(0, 19, "Bbox - ymin : "+str(round(y, 2)), 
         fontweight ="bold")
ax1.text(0, 18, "Bbox - width : "+str(w), 
         fontweight ="bold")
ax1.text(0, 17, "Bbox - height : "+str(h), 
         fontweight ="bold")
fig.suptitle('matplotlib.axes.Axes.get_position()\
function Example\n', fontweight ="bold")
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
   
Z = np.random.rand(6, 30)
   
fig, ax1 = plt.subplots()
   
ax1.pcolor(Z)
  
chartBox = ax1.get_position()
x, y, x1, y1 = chartBox.x0, chartBox.y0, chartBox.x1, chartBox.y1
  
ax1.text(4, 6.35, "Bbox - xmin : "+str(x),
         fontweight ="bold")
ax1.text(19, 6.35, "Bbox - ymin : "+str(round(y, 2)),
         fontweight ="bold")
ax1.text(4, 6.15, "Bbox - xmax : "+str(x1), 
         fontweight ="bold")
ax1.text(19, 6.15, "Bbox - ymax : "+str(y1), 
         fontweight ="bold")
fig.suptitle('matplotlib.axes.Axes.get_position()\
function Example\n', fontweight ="bold")
plt.show()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads