Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展。 Artist类包含用于呈现到FigureCanvas中的对象的Abstract基类。图中所有可见元素都是Artist的子类。
matplotlib.artist.Artist.get_contains()方法
matplotlib库的艺术家模块中的get_contains()方法用于返回艺术家的自定义包含函数。
用法:Artist.get_contains(self)
参数:此方法不接受任何参数。
返回:此方法返回艺术家的自定义包含函数。
以下示例说明了matplotlib中的matplotlib.artist.Artist.get_contains()函数:
范例1:
# Implementation of matplotlib function
from matplotlib.artist import Artist
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())
Artist.set_contains(ax2, picker = onpick1)
ax2.text(0.5, 0.8,
"Value Return:"
+ str(Artist.get_contains(ax2)),
fontweight ="bold", fontsize = 10)
fig.suptitle('matplotlib.artist.Artist.get_contains() \
function Example', fontweight ="bold")
plt.show()
输出:
范例2:
# Implementation of matplotlib function
from matplotlib.artist import Artist
# Implementation of matplotlib function
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import numpy as np
from numpy.random import rand
fig, ax = plt.subplots()
ax.plot(rand(100), rand(100), 'o')
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()
Artist.set_contains(ax, picker = line_picker)
ax.text(0.1, 0.8,
"Value Return:"
+ str(Artist.get_contains(ax)),
fontweight ="bold", fontsize = 10)
fig.suptitle('matplotlib.artist.Artist.get_contains() \
function Example', fontweight ="bold")
plt.show()
输出:
注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 Matplotlib.artist.Artist.get_contains() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。