Matplotlib.axes.Axes.set_position() 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.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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:




My Personal Notes arrow_drop_up

Small things always make you to think big

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.