Matplotlib.axes.Axes.get_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.get_contains() Function
The Axes.get_contains() function in axes module of matplotlib library is used to return the custom contains function of the artist.
Syntax: Axes.get_contains(self)
Parameters: This method does not accept any parameters.
Returns: This method returns the custom contains function of the artist.
Below examples illustrate the matplotlib.axes.Axes.get_contains() function in matplotlib.axes:
Example 1:
# Implementation of matplotlib function import matplotlib.pyplot as plt from matplotlib.lines import Line2D import numpy as np from numpy.random import rand fig, ax2 = plt.subplots() 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) ax2.text(0.5, 0.9, "Value Return : "+ str(ax2.get_contains()), fontweight ="bold", fontsize = 10) fig.suptitle('matplotlib.axes.Axes.get_contains() function\ Example', fontweight ="bold") plt.show() |
Output:
Example 2:
# Implementation of matplotlib function import matplotlib.pyplot as plt from matplotlib.lines import Line2D import numpy as np from numpy.random import rand fig, ax2 = plt.subplots() 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) ax2.text(0.5, 0.9, "Value Return : "+ str(ax2.get_contains()), fontweight ="bold", fontsize = 10) fig.suptitle('matplotlib.axes.Axes.get_contains() function\ Example', fontweight ="bold") plt.show() |
Output:




