Python中的 Matplotlib.axes.Axes.get_contains()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.get_contains()函数
matplotlib 库的 axes 模块中的Axes.get_contains()函数用于返回艺术家的自定义包含函数。
Syntax: Axes.get_contains(self)
Parameters: This method does not accept any parameters.
Returns: This method returns the custom contains function of the artist.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.get_contains()函数:
示例 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()
输出:
示例 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()
输出: