Matplotlib.artist.artist.set_contains() - 定义artist的自定义包含测试

Matplotlib.artist.artist.set_contains()

Python matplotlib库的artist模块中的set_contains()方法用于定义artist的自定义包含测试。

语法:Artist.set_contains(self, picker)

参数:此方法接受以下参数,讨论如下:

  • picker:这个参数是自定义picker函数,用于评估一个事件是否在artist内部。

Returns:该方法不返回任何值。

下面的例子说明了matplotlib中的matplotlib.artist.artist.set_contains()函数:

示例1

# Implementation of matplotlib function
from matplotlib.artist import Artist
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()) 
   
Artist.set_contains(ax2, picker = onpick1)
fig.canvas.mpl_connect('pick_event', onpick1) 
         
fig.suptitle('matplotlib.artist.Artist.set_contains() \
function Example', fontweight ="bold") 
  
plt.show()

输出:

Matplotlib.artist.artist.set_contains()

示例2

# Implementation of matplotlib function
from matplotlib.artist import Artist
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') 
  
Artist.set_contains(ax, picker = line_picker)
  
fig.canvas.mpl_connect('pick_event', onpick2) 
         
fig.suptitle('matplotlib.artist.Artist.set_contains() \
function Example', fontweight ="bold") 
  
plt.show()

输出:

Matplotlib.artist.artist.set_contains()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Matplotlib.artist