當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python Matplotlib.axis.Axis.get_contains()用法及代碼示例


Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。它是Python中令人驚歎的可視化庫,用於數組的2D圖,並用於與更廣泛的SciPy堆棧配合使用。

matplotlib.axis.Axis.get_contains()函數

matplotlib庫的axis模塊中的Axis.get_contains()函數用於獲取藝術家的自定義包含函數。

用法: Axis.get_contains(self) 
 

參數:此方法不接受任何參數。

返回值:此方法返回藝術家的自定義包含函數。



以下示例說明了matplotlib.axis中的matplotlib.axis.Axis.get_contains()函數:

範例1:

Python3

# Implementation of matplotlib function 
from matplotlib.axis import Axis 
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())  
     
Axis.set_contains(ax2, picker = onpick1)  
    
ax2.text(0.5, 0.8,   
         "Value Return:"
         + str(Axis.get_contains(ax2)),    
         fontweight ="bold", fontsize = 10)  
  
fig.suptitle("""matplotlib.axis.Axis.get_contains() 
function Example\n""", fontweight ="bold")   
    
plt.show()

輸出:

範例2:

Python3

# Implementation of matplotlib function 
from matplotlib.axis import Axis 
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()   
     
Axis.set_contains(ax, picker = line_picker)  
    
ax.text(0.1, 0.8,   
         "Value Return:"
         + str(Axis.get_contains(ax)),    
         fontweight ="bold", fontsize = 10) 
  
fig.suptitle("""matplotlib.axis.Axis.get_contains() 
function Example\n""", fontweight ="bold")   
    
plt.show()

輸出:




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Matplotlib.axis.Axis.get_contains() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。