Friday, September 26, 2025
HomeLanguagesMatplotlib.axes.Axes.set_contains() in Python

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

The Axes.set_contains() function in axes module of matplotlib library is used to define a custom contains test for the artist.

Syntax: Axes.set_contains(self, picker)

Parameters: This method accept the following parameters as discussed below:

  • picker : This parameter is the custom picker function to evaluate if an event is within the artist.

Returns: This method does not return any value.

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

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle
from matplotlib.text import Text
from matplotlib.image import AxesImage
import numpy as np
from numpy.random import rand
  
  
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.set_ylabel('ylabel', picker = True,
               bbox = dict(facecolor ='red'))
  
line, = ax1.plot(rand(100), 'go-')
  
ax2.bar(range(10), rand(10), picker = True)
  
for label in ax2.get_xticklabels(): 
    label.set_picker(True)
  
def onpick1(event):
      
    if isinstance(event.artist, Line2D):
        thisline = event.artist
        xdata = thisline.get_xdata()
        ydata = thisline.get_ydata()
        ind = event.ind
        print('onpick1 line:', np.column_stack([xdata[ind],
                                                ydata[ind]]))
          
    elif isinstance(event.artist, Rectangle):
        patch = event.artist
        print('onpick1 patch:', patch.get_path())
          
    elif isinstance(event.artist, Text):
        text = event.artist
        print('onpick1 text:', text.get_text())
  
ax2.set_contains(onpick1)
fig.suptitle('matplotlib.axes.Axes.set_contains() function\
Example', fontweight ="bold")
  
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle
from matplotlib.text import Text
from matplotlib.image import AxesImage
import numpy as np
from numpy.random import rand
  
  
def line_picker(line, mouseevent):
      
    if mouseevent.xdata is None:
        return False, dict()
      
    xdata = line.get_xdata()
    ydata = line.get_ydata()
    maxd = 0.05
    d = np.sqrt(
        (xdata - mouseevent.xdata)**2 + (ydata - mouseevent.ydata)**2)
  
    ind, = np.nonzero(d <= maxd)
      
    if len(ind):
          
        pickx = xdata[ind]
        picky = ydata[ind]
        props = dict(ind = ind, pickx = pickx, picky = picky)
        return True, props
      
    else:
        return False, dict()
  
def onpick2(event):
      
    print('Result :', event.pickx, event.picky)
  
fig, ax = plt.subplots()
ax.plot(rand(100), rand(100), 'o')
  
ax.set_contains(picker = line_picker)
  
fig.canvas.mpl_connect('pick_event', onpick2)
  
fig.suptitle('matplotlib.axes.Axes.set_contains()\
function Example', fontweight ="bold")
  
plt.show()


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32320 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6683 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6756 POSTS0 COMMENTS
Umr Jansen
6762 POSTS0 COMMENTS