Open In App

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

The Axes.set_xbound() function in axes module of matplotlib library is used to set the lower and upper numerical bounds of the x-axis.

Syntax: Axes.set_xbound(self, lower=None, upper=None)

Parameters: This method accepts the following parameters.

  • lower, upper: These parameters are the lower and upper bounds. If None, the respective axis bound is not modified.

Returns:This method returns the following

  • lower, upper :This returns the new lower and upper x-axis bounds.

Note : This function can be used in place of set_xlim in various conditions.

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

Example 1:




# Implementation of matplotlib function
from matplotlib.widgets import Cursor
import numpy as np
import matplotlib.pyplot as plt
   
   
np.random.seed(19680801)
   
fig, ax = plt.subplots()
  
x = 2 * np.cos(2 * np.pi*(np.arange(-3, 3, 0.005)))
y = 2 * np.sin(2 * np.pi*(np.arange(0, 6, 0.005)))
ax.plot(x, y, 'g')
ax.set_xbound(-4, 4)
  
ax.set_title('matplotlib.axes.Axes.set_xbound() \
Example\n', fontsize = 14, fontweight ='bold')
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
   
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
ax1.set(xlim =(-1.0, 1.0), 
        ylim =(-1.0, 1.0), 
        autoscale_on = False)
  
ax2.set(xlim =(-0.5, 0.5), 
        ylim =(-0.5, 0.5), 
        autoscale_on = False)
   
x, y, s, c = np.random.rand(4, 200)
s *= 200
   
ax1.scatter(x, y, s, c)
ax2.scatter(x, y, s, c)
   
   
def GFG(event):
      
    if event.button != 1:
        return
      
    x, y = event.xdata, event.ydata
    ax2.set_xbound(x - 0.5, x + 0.5)
    ax2.set_ybound(y - 0.5, y + 0.5)
    fig2.canvas.draw()
   
fig1.canvas.mpl_connect('button_press_event',
                        GFG)   
ax1.set_title('matplotlib.axes.Axes.set_xbound()\
Example\n Original Window ',
             fontsize = 14, fontweight ='bold')
  
ax2.set_title('Zoomed Window',
             fontsize = 14, fontweight ='bold')
plt.show()


Output:



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

Similar Reads