Open In App

Matplotlib.axes.Axes.set_position() in Python

Last Updated : 19 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
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.set_position() Function

The Axes.set_position() function in axes module of matplotlib library is used to set the axes position.
Syntax: Axes.set_position(self) Parameters: This method accepts the following parameters.
  • pos : This parameter is the new position of the in Figure coordinates.
  • which : This parameter is used to determines which position variables to change.
Return value: This method does not return any value.
Below examples illustrate the matplotlib.axes.Axes.set_position() function in matplotlib.axes: Example 1: Python3
# 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, ax = plt.subplots()

ax.plot(x, y, "go-", label ='Line 1', )
ax.plot(x, y2, "o-", label ='Line 2')

chartBox = ax.get_position()
ax.set_position([chartBox.x0, chartBox.y0,
                 chartBox.width,
                 chartBox.height * 0.6])

ax.legend(loc ='upper center',
          bbox_to_anchor =(0.5, 1.45),
          shadow = True, ncol = 1)
 
fig.suptitle('matplotlib.axes.Axes.set_position()\
function Example', fontweight ="bold")
plt.show()
Output: Example 2: Python3
# 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, (ax, ax1) = plt.subplots(1, 2)
 
ax.pcolor(Z)
ax1.pcolor(Z)

chartBox = ax1.get_position()
ax1.set_position([chartBox.x0, 
                  chartBox.y0,
                  chartBox.width,
                  chartBox.height * 0.6])

ax.set_title("Original Window")
ax1.set_title("Modified  Window")
 
fig.suptitle('matplotlib.axes.Axes.set_position()\
function Example', fontweight ="bold")
plt.show()
Output:

Next Article
Article Tags :
Practice Tags :

Similar Reads