Skip to content
Related Articles

Related Articles

Matplotlib.axes.Axes.get_xlim() in Python

Improve Article
Save Article
Like Article
  • Last Updated : 19 Apr, 2020

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

The Axes.get_xlim() function in axes module of matplotlib library is used to get the x-axis view limits.

Syntax: Axes.get_xlim(self)

Returns:This method returns the following

  • left, right :This returns the current x-axis limits in data coordinates.

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

Example 1:

#Implementation of matplotlib function
from matplotlib.widgets import Cursor
import numpy as np
import matplotlib.pyplot as plt
fig, (ax,ax1) = plt.subplots(1 , 2 , facecolor='#A0F0CC')
x, y = 4*(np.random.rand(2, 100) - .5)
ax.plot(x, y, 'g')
ax.set_xlim(-3, 3)
xmin, xmax = ax.get_xlim()
ax.set_title('Original Window', fontsize=10, fontweight='bold')
ax1.plot(x, y, 'g')
ax1.set_xlim(xmin, 2*xmax)
ax1.set_title('Using get_xlim() function',
             fontsize=10, fontweight='bold')
 Example\n',fontsize=10, fontweight='bold')


Example 2:

#Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
fig1, (ax1,ax11) = plt.subplots(1,2)
fig2, (ax2,ax22) = plt.subplots(1,2)
ax1.set(xlim=(-1.0, 1.0), ylim=(-1.0, 1.0), 
ax2.set(xlim=(-0.5, 0.5), ylim=(-0.5, 0.5),
ax11.set(xlim=(-1.0, 1.0), ylim=(-1.0, 1.0),
ax22.set(xlim=(-0.5, 0.5), ylim=(-0.5, 0.5), 
x, y, s, c = np.random.rand(4, 200)
s *= 200
ax1.scatter(x, y, s, c)
ax2.scatter(x, y, s, c)
ax11.scatter(x, y, s, c)
ax22.scatter(x, y, s, c)
def GFG(event):
    if event.button != 1:
    x, y = event.xdata, event.ydata
    ax2.set_xlim(x - 0.5, x + 0.5)
    ax2.set_ylim(y - 0.5, y + 0.5)
    ax22.set_xlim(x - 0.5, x + 0.5)
    ax22.set_ylim(y - 0.5, y + 0.5)
fig1.canvas.mpl_connect('button_press_event', GFG)  
xmin, xmax = ax1.get_xlim()
ax1.set_title('Original Window', fontsize=10
ax11.set_xlim(xmin, 2*xmax)
ax11.set_title('After Using get_xlim() function',
             fontsize=10, fontweight='bold')
xmin, xmax = ax2.get_xlim()
ax2.set_title('Zoomed Window', fontsize=10
ax22.set_xlim(xmin, 2*xmax)
ax22.set_title('After Using get_xlim() function',
             fontsize=10, fontweight='bold')
fig1.suptitle('matplotlib.axes.Axes.get_xlim() \
Example\n',fontsize=10, fontweight='bold')


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!