Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展。它是Python中令人惊叹的可视化库,用于数组的2D图,并用于与更广泛的SciPy堆栈配合使用。
matplotlib.axis.Tick.set_contains()函数
matplotlib库的axis模块中的Tick.set_contains()函数用于定义艺术家的自定义包含测试。
用法: Tick.Axis.set_contains(self, picker)
参数:此方法接受以下参数。
- picker:此参数是自定义选择器函数,用于评估事件是否在艺术家中。
返回值:此方法不返回任何值。
以下示例说明了matplotlib.axis中的matplotlib.axis.Tick.set_contains()函数:
范例1:
Python3
# Implementation of matplotlib function
from matplotlib.axis import Tick
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())
Tick.set_contains(ax2, picker = onpick1)
fig.canvas.mpl_connect('pick_event', onpick1)
fig.suptitle('matplotlib.axis.Tick.set_contains() \
function Example', fontweight ="bold")
plt.show()
输出:
onpick1 text:ylabel onpick1 patch:Path(array([[0., 0.], [1., 0.], [1., 1.], [0., 1.], [0., 0.]]), array([ 1, 2, 2, 2, 79], dtype=uint8)) onpick1 patch:Path(array([[0., 0.], [1., 0.], [1., 1.], [0., 1.], [0., 0.]]), array([ 1, 2, 2, 2, 79], dtype=uint8))
范例2:
Python3
# Implementation of matplotlib function
from matplotlib.axis import Tick
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')
Tick.set_contains(ax, picker = line_picker)
fig.canvas.mpl_connect('pick_event', onpick2)
fig.suptitle('matplotlib.axis.Tick.set_contains() \
function Example', fontweight ="bold")
plt.show()
输出:
相关用法
- Python Wand function()用法及代码示例
- Python int()用法及代码示例
- Python tell()用法及代码示例
- Python map()用法及代码示例
- Python str()用法及代码示例
- Python dir()用法及代码示例
- Python id()用法及代码示例
- Python hex()用法及代码示例
- Python now()用法及代码示例
- Python sum()用法及代码示例
- Python oct()用法及代码示例
- Python ord()用法及代码示例
- Python cmp()用法及代码示例
- Python join()用法及代码示例
- Python fsum()用法及代码示例
- Python strftime()用法及代码示例
- Python isdisjoint()用法及代码示例
注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 Matplotlib.axis.Tick.set_contains() function in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。